美文网首页
网格视图、下拉列表框、滚动视图、选项卡

网格视图、下拉列表框、滚动视图、选项卡

作者: 被罚站的树 | 来源:发表于2019-05-18 11:03 被阅读0次

网格视图

image.png image.png
<GridView
            android:id="@+id/gV"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:numColumns="auto_fit"
            android:verticalSpacing="5dp">

        </GridView>

方法1:

        GridView gridView=findViewById(R.id.gV);
        gridView.setAdapter(new ImageAdapter(this));
public class ImageAdapter extends BaseAdapter{

        private Context mContext;
        public ImageAdapter(Context c){
            mContext=c;
        }
        @Override
        public int getCount() {
            return pic.length;   //返回图片资源的长度
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if(convertView==null){            //判断传入的imageView是否为空,空则新创
                imageView=new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(400,350));//展示的图片大小
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);//缩放比例
            }else {
                imageView=(ImageView) convertView;
            }
            imageView.setImageResource(pic[position]);
            return imageView;
        }
        @Override
        public Object getItem(int position) {
            return null;
        }
        @Override
        public long getItemId(int position) {
            return 0;
        }
    }

方法2:

cell.xml layout
 <ImageView
        android:id="@+id/image"
        android:layout_width="200dp"
        android:layout_height="150dp" />
 GridView gridView=findViewById(R.id.gV);

      List<Map<String,Object>> listitem=new ArrayList<Map<String, Object>>();
        for (int i=0;i<pic.length;i++){
               Map<String,Object>map=new HashMap<String, Object>();
               map.put("image",pic[i]);
               listitem.add(map);
       }
       SimpleAdapter simpleAdapter=new SimpleAdapter
              (this,listitem,R.layout.cell,new String[]{"image"},new int[]{R.id.image});

        gridView.setAdapter(simpleAdapter);

下拉列表框

values中的array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="ctype">
        <item>全部</item>
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
        <item>5</item>
    </string-array>
</resources>

 <Spinner
        android:id="@+id/sP"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:entries="@array/ctype"
        ></Spinner>

    <Spinner
        android:layout_toRightOf="@id/sP"
        android:id="@+id/sP2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ></Spinner>
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String []ctype=new String[]{
          "1","2","3","4"
        };

        ArrayAdapter<String> adapter=new ArrayAdapter<String>
                    (this,android.R.layout.simple_spinner_item,ctype);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Spinner spinner=findViewById(R.id.sP2);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                String str=parent.getItemAtPosition(position).toString();
                Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


    }
}

列表视图

   <ImageView
        android:id="@+id/image"
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:maxWidth="72dp"
        android:maxHeight="72dp"
        />
    <TextView
        android:id="@+id/title"
        android:padding="10dp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/lV"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >  
    </ListView>
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        
        int[] imageId=new int[]{
                R.mipmap.a, R.mipmap.b, R.mipmap.c,
                R.mipmap.h, R.mipmap.e, R.mipmap.d,
                R.mipmap.i, R.mipmap.j, R.mipmap.k
        };
        String[] title=new String[]{
          "OJBK1","OJBK2","OJBK3","OJBK4","OJBK5","OJBK6","OJBK7",
                "OJBK8","OJBK9"
    };
ListView listView=(ListView) findViewById(R.id.lV);
        List<Map<String,Object>> listitem=new ArrayList<Map<String, Object>>();
        for(int i=0;i<imageId.length;i++){
            Map<String,Object>map= new HashMap<String,Object>();
            map.put("image",imageId[i]);
            map.put("name",title[i]);
            listitem.add(map);
        }

        SimpleAdapter adapter=new SimpleAdapter(this,listitem,
                R.layout.activity_main,new String[]{"name","image"},new int[]{R.id.title,R.id.image});

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String,Object>map=(Map<String,Object>)parent.getItemAtPosition(position);
                Toast.makeText(MainActivity.this,map.get("name").toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }

滚动视图
纯JAVA实现


image.png
<resources>
    <string name="app_name">App12</string>

    <string name="cidian">

        OJBK OJBK OJBK
        OJBK OJBK OJBK\n
        OJBK OJBK OJBK\n
        OJBK OJBK OJBK\n

        OJBK OJBK OJBK\n
        OJBK OJBK OJBK\n
        OJBK OJBK OJBK\n
        OJBK OJBK OJBK\n

        OJBK OJBK OJBK\n

    </string>

</resources>

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout ll=findViewById(R.id.ll);
        LinearLayout ll2=new LinearLayout(MainActivity.this);
        ll2.setOrientation(LinearLayout.VERTICAL);
        ScrollView scrollView=new ScrollView(MainActivity.this);
        ll.addView(scrollView);
        scrollView.addView(ll2);
        ImageView imageView=new ImageView(MainActivity.this);
        imageView.setImageResource(R.mipmap.e);
        ll2.addView(imageView);
        TextView textView=new TextView(MainActivity.this);
        textView.setText(R.string.cidian);
        ll2.addView(textView);
    }
}

Xml实现

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:id="@+id/ll"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/e"
            />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/c"
                />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cidian"/>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

选项卡


image.png
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/left"
    android:orientation="vertical">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@mipmap/e"
        />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/right"
    android:orientation="vertical">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@mipmap/a"
        />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@android:id/tabhost"
    >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </TabWidget>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    </FrameLayout>
    </LinearLayout>
</TabHost>
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabHost tabHost=findViewById(android.R.id.tabhost);
        tabHost.setup();//初始化
        LayoutInflater inflater=LayoutInflater.from(this);//声明实例化LayoutInflater
        inflater.inflate(R.layout.tab1,tabHost.getTabContentView());//利用LayoutInflater加载各个选项卡的布局文件
        inflater.inflate(R.layout.tab2,tabHost.getTabContentView());
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("1").setContent(R.id.left));//添加标签页,显示文字,内容
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("2").setContent(R.id.right));
    }
}

相关文章

网友评论

      本文标题:网格视图、下拉列表框、滚动视图、选项卡

      本文链接:https://www.haomeiwen.com/subject/mkrnaqtx.html