我在顶部显示图像,在底部显示文本作为框架布局。每个帧布局都在网格单元格下。 我正在重叠文本和图像。我希望图像为顶部,文本为底部,没有任何重叠。有什么帮助吗?
我附上了我的代码。
item_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dp"
android:paddingLeft="2dp"
android:paddingRight="0dp"
android:paddingTop="2dp" >
<ImageView
android:id="@+id/home_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_gravity="top|center"
android:adjustViewBounds="true"
android:clickable="true"
android:contentDescription="Image Description"
android:scaleType="centerInside" />
<TextView
android:id="@+id/home_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
style="@style/mainGridItemStyle"
android:gravity="center"
android:maxLines="1"
android:paddingLeft="2dip"
android:paddingRight="2dp" />
</FrameLayout>
main_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/gridViewMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
</LinearLayout>
Java代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_grid);
setOptions("g-app", null);
gridView = (GridView) findViewById(R.id.gridViewMain);
gridView.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context context) {
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.item_grid, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.home_item_label);
HashMap<String, String> item = data.get(position);
textView.setText(item.get("name"));
//textView.setBackgroundColor(Color.WHITE);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.home_item_image);
imageView.setImageResource(R.drawable.contacts1);
} else {
gridView = (View) convertView;
}
return gridView;
}
@Override
public int getCount() {
return data.size();
}
@Override
public String getItem(int position) {
return data.get(position).toString();
}
@Override
public long getItemId(int position) {
return position;
}
}
答案 0 :(得分:0)
首先,我认为你使用layout_gravity错误。你们都设置bottom|center.
中心覆盖底部。你应该做bottom|center_horizontal
如果要在图像下方显示文本,则应尝试使用LinearLayout。使用FrameLayout,无论您做什么,都会在图像上绘制文本。