问题描述
我已经为类似的解决方案,但没有到处搜寻,似乎为我工作。
I have been searching everywhere for similar solutions but none seem to work for me.
在我的第一个画面我都会有它由1列和3行的垂直一个gridview。每一行都将有一个ImageView的和文本视图顶部的部分透明的TextView。该imageviews跨越屏幕宽度的罚款。我唯一的问题是,3图像视图不跨越整个屏幕垂直,还有空间其间行,虽然我已经尝试了许多方法来解决这个问题。我会后我的XML文件和code:
On my first screen I will have a gridview which consists of 1 column and 3 rows vertically. Each row will have an imageview and a partially transparent textview on top of the text view. The imageviews span the screen width fine. My only problem is that the 3 image views do not span the full screen vertically, there is space inbetween rows, although I have tried many methods to fix this. I will post up my xml files and code:
row_layout.xml - 这是gridview的项目
row_layout.xml - this is the gridview items
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/item_image"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_width="fill_parent"
android:layout_height="180dp"
android:src="@drawable/season1">
</ImageView>
<TextView
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/item_image"
android:layout_alignTop="@+id/item_image"
android:layout_alignRight="@+id/item_image"
android:layout_alignBottom="@+id/item_image"
android:gravity="bottom|left"
android:textSize="30sp"
android:textAlignment="center"
android:lines="1"
android:layout_marginTop="145dp"
android:background="#99000000">
</TextView>
main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<GridView
android:id="@+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:padding="0dp"
android:gravity="center"
android:numColumns="1"
android:fitsSystemWindows="true"
android:stretchMode="columnWidth" >
</GridView>
我的自定义适配器:
public class CustomGridViewAdapter extends ArrayAdapter<Item> {
Context context;
int layoutResourceId;
ArrayList<Item> data = new ArrayList<Item>();
public CustomGridViewAdapter(Context context, int layoutResourceId, ArrayList<Item> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecordHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
row.setMinimumHeight(MainActivity.height1/3);
holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}
Item item = data.get(position);
holder.txtTitle.setText(item.getTitle());
holder.imageItem.setImageBitmap(item.getImage());// my image
return row;
}
static class RecordHolder {
TextView txtTitle;
ImageView imageItem;
}
}
MainActivity:
MainActivity:
public class MainActivity extends ActionBarActivity {
GridView gridView;
ArrayList<Item> gridArray = new ArrayList<Item>();
CustomGridViewAdapter customGridAdapter;
public static int height1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
ActionBar ab = getActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowHomeEnabled(false);
Resources res = this.getResources();
Bitmap bMap = BitmapFactory.decodeResource(res, R.drawable.actionbar);
BitmapDrawable actionBarBackground = new BitmapDrawable(res, bMap);
ab.setBackgroundDrawable(actionBarBackground);
//set grid view item
Bitmap season1Icon = BitmapFactory.decodeResource(res, R.drawable.season1);
Bitmap season2Icon = BitmapFactory.decodeResource(res, R.drawable.season2);
Bitmap season3Icon = BitmapFactory.decodeResource(res, R.drawable.season3);
gridArray.add(new Item(season1Icon,"Season 1"));
gridArray.add(new Item(season2Icon,"Season 2"));
gridArray.add(new Item(season3Icon,"Season 3"));
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
height1 = metrics.heightPixels;
gridView = (GridView) findViewById(R.id.gridView1);
customGridAdapter = new CustomGridViewAdapter(this, R.layout.row_grid, gridArray);
gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent a = new Intent(MainActivity.this, House.class);
a.putExtra("Position", position);
startActivity(a);
}
});
}
}
任何帮助将大大AP preciated,我花了几个小时试图解决这个问题!
Any help will be greatly appreciated, I've spent hours trying to fix this!
推荐答案
动态设置您的ImageView的布局参数,可以,一旦你知道了屏幕的高度。
Dynamically set the layout params of your imageview, once you know the height of the screen.
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
//row.setMinimumHeight(MainActivity.height1/3); //don't need this, since wrap content will make the row height match your image view's
holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
holder.imageItem.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, MainActivity.height1/3);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}
这篇关于3行imageviews那充满整个屏幕创建的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!