网格视图
image.png
image.png
android:id="@+id/gV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:numColumns="auto_fit"
android:verticalSpacing="5dp">
方法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
android:id="@+id/image"
android:layout_width="200dp"
android:layout_height="150dp" />
GridView gridView=findViewById(R.id.gV);
List> listitem=new ArrayList>();
for (int i=0;i
Mapmap=new HashMap();
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
全部
1
2
3
4
5
android:id="@+id/sP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/ctype"
>
android:layout_toRightOf="@id/sP"
android:id="@+id/sP2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
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 adapter=new ArrayAdapter
(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) {
}
});
}
}
列表视图
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"
/>
android:id="@+id/title"
android:padding="10dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:id="@+id/lV"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
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> listitem=new ArrayList>();
for(int i=0;i
Mapmap= new HashMap();
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) {
Mapmap=(Map)parent.getItemAtPosition(position);
Toast.makeText(MainActivity.this,map.get("name").toString(), Toast.LENGTH_SHORT).show();
}
});
}
滚动视图
纯JAVA实现
image.png
App12
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
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实现
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"
>
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/e"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/c"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cidian"/>
选项卡
image.png
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">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/e"
/>
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">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/a"
/>
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"
>
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
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));
}
}