我正在开发Android应用程序我需要在不同设备上安装此应用程序,因此我需要布局完全响应。
我的一项活动是网格视图,其中包含两列,每列包含一个" square" (图片)。
问题在于我无法适应所有设备(小型,普通型,大型等)的播放。
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent i = new Intent();
switch (position) {
case 0:
i.setClass(getApplicationContext(), Bacheca.class);
startActivity(i);
break;
case 1:
i.setClass(getApplicationContext(), Turni.class);
startActivity(i);
break;
case 2:
i.setClass(getApplicationContext(), Utility.class);
startActivity(i);
break;
case 3:
i.setClass(getApplicationContext(), Contatti.class);
startActivity(i);
break;
case 4:
i.setClass(getApplicationContext(), ScriviBacheca.class);
i.putExtra("user_id", id_user);
i.putExtra("nome", cognome);
i.putExtra("cognome", nome);
startActivity(i);
break;
case 5:
i.setClass(getApplicationContext(), Info.class);
i.putExtra("user_id", id_user);
startActivity(i);
break;
}
}
});
}else{
connectivityMessage("Nessuna connessione presente, assicurati di avere il traffico dati oppure il Wi-Fi attivato e riprova.");
}
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(2, 2, 2, 2);
int screenLayout = getApplicationContext().getResources().getConfiguration().screenLayout;
screenLayout &= Configuration.SCREENLAYOUT_SIZE_MASK;
switch (screenLayout) {
case Configuration.SCREENLAYOUT_SIZE_SMALL:
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
imageView.setLayoutParams(new GridView.LayoutParams(330, 330));
break;
case Configuration.SCREENLAYOUT_SIZE_LARGE:
imageView.setLayoutParams(new GridView.LayoutParams(280, 280));
break;
case Configuration.SCREENLAYOUT_SIZE_XLARGE:
imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
break;
default:
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
break;
}
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.bacheca, R.drawable.turni,
R.drawable.utility, R.drawable.contatti,
R.drawable.invia, R.drawable.info
};
}
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="am.pacrocebianca.MainActivity"
tools:showIn="@layout/activity_main">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="2"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</RelativeLayout>
此解决方案适用于普通设备和大型设备,但不适用于小型设备,因为布局与普通布局相同。
这是我用来调试我的应用的设备列表:
小型手机:Sony Xperia U(3,5&#39;&#39;)
普通电话:摩托罗拉moto G 2014(5&#39;&#39;)
大:平板电脑Acer A1-713(7&#39;&#39;)
答案 0 :(得分:1)
您应该将android:numColumns="2"
更改为android:numColumns="auto_fit"
您的GridView
将显示尽可能多的列以填充可用空间。
<GridView
...
android:columnWidth="90dp"
android:numColumns="auto_fit"
/>