• 安卓学习-界面-ui-Gallery


    和spinner类似

    这个是水平排列的,spinner是垂直的

    这个一般是通过滑动来选择,spinner是点击

    属性 方法 说明
     android:animationDuration  setAnimationDuration(int animationDurationMillis) 

     设置布局变化时动画的转换所需的时间

    不知道有什么作用

     android:gravity  setGravity(int gravity)  对齐方式
     android:spacing  setSpacing(int spacing)  2图片之间的间隔
     android:unselectedAlpha  setUnselectedAlpha(float unselectedAlpha)

     没选中的图片的透明度0-1

    例子1

    item.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" />
    </RelativeLayout>
    View Code

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <Gallery
            android:id="@+id/gallery1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
             />
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    View Code

    MainActivity.java

    public class MainActivity extends Activity {
    
        int[] images=new int[]{
                R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5,
                R.drawable.pic6,R.drawable.pic7,R.drawable.pic8,R.drawable.pic9,R.drawable.pic10
        };
        ImageView iv1;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            iv1=(ImageView)findViewById(R.id.imageView1);
            
            List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
            for(int i=0;i<10;i++){
                Map<String, Object> map1 = new HashMap<String, Object>();
                map1.put("pic", images[i]);
                items.add(map1);
            }
            
            SimpleAdapter sa = new SimpleAdapter(MainActivity.this, items,
                    R.layout.item, new String[] { "pic"}, new int[] {
                            R.id.imageView1});
            Gallery gallery=(Gallery)findViewById(R.id.gallery1);
            
            gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
    
                @Override
                public void onItemSelected(AdapterView<?> parent, View view,
                        int position, long id) {
                    
                    iv1.setImageResource(images[position]);
                    
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    // TODO 自动生成的方法存根
                    
                }
                
            });
    
            gallery.setAdapter(sa);
            
        
        }
    }
    View Code
  • 相关阅读:
    sql注入漏洞与防范
    微信小程序-工具,弹出当前系统代理不是安全代理处理方法
    微信小程序-02 小程序关注组件
    微信小程序-01 小数保留二位
    http 转hhttps
    php 函数-ksort
    iOS 原生二维码扫描
    iOS 打包错误 all-product-headers.yaml' not found
    iOS Tableview点击cell 会往上跳
    WKWebView 使用
  • 原文地址:https://www.cnblogs.com/weijj/p/3957419.html
Copyright © 2020-2023  润新知