我使用gridview在全屏显示两列和三行中的6个项目。但它始终显示在scrollview中。为此,找到总屏幕高度(像素)然后在每行中指定(1/3)高度但它始终占据整个显示高度,包括状态栏和状态栏。动作栏高度。如何找到没有状态的屏幕高度&行动吧。我在各种博客中探讨了它,但没有工作。我也支持libray v4& v7 to sho map&行动吧。
activity_main.xml中
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:numColumns="2"
android:gravity="center"
android:columnWidth="150dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/grid"
android:background="#A1A1A1"
android:horizontalSpacing="1dp"
android:verticalSpacing="1dp"/>
每个网格视图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:background="#BABABA"
android:orientation="vertical">
<ImageView
android:id="@+id/grid_image"
android:layout_width="65dp"
android:layout_height="65dp"
android:gravity="center"
android:layout_gravity="center"/>
<TextView
android:id="@+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="19sp" />
MainActivity.java
public static int height1;
GridView grid;
String[] web = {"Browse","Important Date","Paper Status","Running Session","Accepted Paper","Map"};
int[] imageId ={R.drawable.ic_action_search,R.drawable.running_session,R.drawable.paper_status,R.drawable.my_program,R.drawable.reading_list,R.drawable.ic_action_map};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2C5197")));
// determined the screen pixel
DisplayMetrics metrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
height1=metrics.heightPixels;
//determined the screen pixel
CustomGrid adapter = new CustomGrid(MainActivity.this, web, imageId);
grid=(GridView)findViewById(R.id.grid);
grid.setAdapter(adapter);
} }
这是适配器类 CustomGrid.java
public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String[] web;
private final int[] Imageid;
public CustomGrid(Context c,String[] web,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.web = web;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return web.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.each_grid_view, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
textView.setText(web[position]);
imageView.setImageResource(Imageid[position]);
//for match with the screen
grid.setLayoutParams(new GridView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,MainActivity.height1/3));
} else {
grid = (View) convertView;
}
return grid;
}
}
答案 0 :(得分:0)
我不会以编程方式计算指标,而是让操作系统自动计算滚动条是否应该出现,具体取决于您的网格视图是大于还是小于实际屏幕。 通常,使用ScrollView并将GridView嵌套在其中:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
也许您还应该将GridView的layout_height配置更改为 wrap_content ,这样它就不会填满整个高度:
<GridView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
Lubos