我的带有网格视图的android布局在480 x 854像素(~196 ppi像素密度)设备上无法正常显示。请建议我在运行时使用任何方法处理布局。
答案 0 :(得分:1)
Android使用两个常规属性对设备屏幕进行分类:大小 和密度。您应该期望安装您的应用程序 屏幕尺寸和密度各不相同的设备。
要优化不同屏幕尺寸的用户体验,您应为要支持的每种屏幕尺寸创建唯一的布局XML文件。每个布局都应保存到相应的资源目录中。
更多信息,您可以访问官方指南
答案 1 :(得分:0)
@ N.shah,对于Gridview,您应该使所有视图都是动态的,以便所有单元格准确地出现在每个设备上 我们如何做到这一点 就像我们有4行2列 所有4行都应显示在窗口上 1.找出设备的宽度和高度 2.假设高度为X.找出状态栏的高度,即Y和动作栏Z. 3.找出我们必须显示网格视图的确切高度,即 X = X-(Y + Z) 4.将X除以否。你想要显示的行,以找出行高,即R. R = X / 4(其中4是行数) 5.创建要在gridview中显示的xml
custom_gridview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ll_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_marginBottom="@dimen/menu_grid_vertical_spacing"
android:orientation="vertical"
android:weightSum="4.5" >
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.9"
android:gravity="center" >
<ImageView
android:id="@+id/img_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:padding="@dimen/menu_page_padding"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.4" />
<com.slinfy.ikharelimiteduk.custom.CustomTextViewSegoeUISemiBold
android:id="@+id/txt_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.2"
android:gravity="top|center_horizontal"
android:text="@string/app_name"
android:textColor="@android:color/white"
android:textSize="@dimen/app_menu_text_size" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/menu_grid_vertical_spacing"
android:layout_alignParentBottom="true"
android:background="#2E3A60" />
</RelativeLayout>
制作适配器
GridViewAdapter
import com.slinfy.ikharelimiteduk.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
@SuppressLint({ "ViewHolder", "InflateParams" })
public class GridViewAdapter extends BaseAdapter {
String[] mMenuNames;
int[] mColors;
Integer[] mMenuIcons;
Context mContext;
private LayoutInflater inflater;
float mGridSize;
float mPadding;
int noOfColums = 4;
public GridViewAdapter(String[] menuNames, Context context, int[] colors, Integer[] menuIcons, float gridSize) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuNames = menuNames;
mMenuIcons = menuIcons;
mColors = colors;
mContext = context;
mGridSize = gridSize;
mPadding = context.getResources().getDimension(R.dimen.menu_grid_vertical_spacing);
}
@Override
public int getCount() {
return mMenuIcons.length;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int index, View arg1, ViewGroup arg2) {
ViewHolder holder = null;
if (arg1 == null) {
holder = new ViewHolder();
arg1 = inflater.inflate(R.layout.custom_gridview, null);
holder.imgMenu = (ImageView) arg1.findViewById(R.id.img_menu);
holder.txtName = (TextView) arg1.findViewById(R.id.txt_name);
holder.layout = (RelativeLayout) arg1.findViewById(R.id.layout);
arg1.setTag(holder);
} else {
holder = (ViewHolder) arg1.getTag();
}
holder.txtName.setText(mMenuNames[index]);
holder.imgMenu.setImageResource(mMenuIcons[index]);
holder.layout.setBackgroundResource(mColors[index]);
holder.layout.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT,
(int) ((mGridSize / noOfColums) - 5/* - (mPadding) - 6 */)));
return arg1;
}
class ViewHolder {
TextView txtName;
ImageView imgMenu;
RelativeLayout layout;
}
}
在主类中设置适配器
GridView grid = (GridView) findViewById(R.id.grid_view);
grid.setAdapter(new GridViewAdapter(menuNames, getApplicationContext(), colors, menuIcons, gridSize));