简体   繁体   English

Android FrameLayout重叠问题

[英]Android FrameLayout Overlap Issue

I am displaying image at top and text at bottom as frame layout. 我在顶部显示图像,在底部显示文本作为框架布局。 Each frame layout is under grid cells. 每个框架布局都在网格单元下方。 I am getting overlaping of text and image. 我正在重叠文本和图像。 I want image as top and text as bottom without any overlap. 我希望图像作为顶部,文本作为底部,没有任何重叠。 Any help? 有什么帮助吗?

I have attached my code as bellow. 我已将我的代码附在下面。

item_grid.xml 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 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 Code: 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;
        }

    }

First of all, I think you use layout_gravity wrong. 首先,我认为您使用layout_gravity错误。 You both set bottom|center. 你们都设置了bottom|center. center overrides bottom. 中心覆盖底部。 You should do like bottom|center_horizontal 你应该喜欢bottom|center_horizontal

If you want to display the text below the Image, you should try LinearLayout. 如果要在图像下方显示文本,则应尝试使用LinearLayout。 With FrameLayout the text will be drawn over the image no matter what you do. 使用FrameLayout,无论您做什么,都将在图像上绘制文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM