Android - 如何让子LinearLayout与父RelativeLayout的高度匹配

时间:2015-08-19 18:50:14

标签: android android-layout android-linearlayout relativelayout

我将这个LinearLayout放在RelativeLayout中。

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#66000000"
    android:gravity="center">

    <ImageView
        android:layout_width="30dp"
        android:layout_height="23dp"
        android:src="@drawable/big_tick" />

</LinearLayout>

我要做的是 - 显示已选择的项目。我设置了LinearLayout的高度和宽度match_parent。它只匹配父(RelativeLayout)的宽度,而不是高度。

这是我打算做的: enter image description here

这是我得到的: enter image description here

LinearLayout没有占据其父级的整个高度。

有两个原因,为什么我使用Relative布局作为父级:

  1. LinearLayout应该是40%黑色的书籍信息(

  2. 要在书的顶部显示此符号

  3. enter image description here

    整个XML看起来像这样:

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal">
    
        <LinearLayout
            android:id="@+id/llBook"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="6dp"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:paddingBottom="5dp"
            android:paddingLeft="11dp"
            android:paddingRight="2dp"
            android:paddingTop="8dp">
    
            <ImageView
                android:id="@+id/ivCover"
                android:layout_width="95dp"
                android:layout_height="146dp" />
    
            <TextView
                android:id="@+id/tvTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:textColor="#232425"
                android:textSize="12sp" />
    
            <TextView
                android:id="@+id/tvAuthor"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:textColor="#848586"
                android:textSize="10sp" />
    
        </LinearLayout>
    
        <ImageView
            android:layout_width="22dp"
            android:layout_height="22dp"
            android:layout_alignRight="@id/llBook"
            android:layout_marginRight="6dp"
            android:background="@drawable/shape_round_book_status"
            android:padding="8dp"
            android:src="@drawable/icon_new" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#66000000"
            android:gravity="center">
    
            <ImageView
                android:layout_width="30dp"
                android:layout_height="23dp"
                android:src="@drawable/big_tick" />
    
        </LinearLayout>
    
    </RelativeLayout>
    

    我在BaseAdapter类中使用这个RelativeLayout,我用它来填充GridView。

    public class LibraryAdapter extends BaseAdapter {
    
    Context context;
    RealmResults<RBook> rBooks;
    LayoutInflater inflater;
    
    public LibraryAdapter(Context context, RealmResults<RBook> rBooks) {
        this.context = context;
        this.rBooks = rBooks;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    @Override
    public int getCount() {
        return rBooks.size();
    }
    
    @Override
    public Object getItem(int position) {
        return rBooks.get(position);
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        RBook rBook = (RBook) getItem(position);
    
        View view = convertView;
    
        if(view == null) {
            // new row is needed to inflate new row
            view = inflater.inflate(R.layout.listitem_library_book, parent, false);
        }
    
        Bitmap bitmap = BitmapFactory.decodeByteArray(rBook.getCover() , 0, rBook.getCover().length);
        ImageView ivCover = (ImageView) view.findViewById(R.id.ivCover);
        ivCover.setImageBitmap(bitmap);
    
        TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        tvTitle.setText(rBook.getTitle());
    
        TextView tvAuthor = (TextView) view.findViewById(R.id.tvAuthor);
        tvAuthor.setText(rBook.getAuthor());
    
        return view;
    }
    

    }

    修改

    Try1 :因为 cj1098 建议我将子LinearLayout更改为RelativeLayout。不幸的是,没有效果

    Try2 :正如挑战者建议的那样,我在我的孩子LinearLayout中使用了这个,但效果是这样的:

    enter image description here

    Try3 :正如codeMagic建议的那样,我已经改变了孩子的LinearLayout的

    gravity="center"
    

    到ImageView的

    layout_gravity="center"
    

    结果,LinearLayout没有占据整个高度。

    所以我的问题是如何让子LinearLayout与父RelativeLayout的高度相匹配

4 个答案:

答案 0 :(得分:3)

尝试将其添加到LinearLayout:

    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"

任何使用方法都可以使用它而不使用LinearLayout(修复drawable“big_tick”的大小):

    <ImageView
        android:layout_width="30dp"
        android:background="#66000000"
        android:layout_height="23dp"
        android:src="@drawable/big_tick" />

答案 1 :(得分:1)

我认为单独使用这种布局很好。事实上,你将它放入ListView正在造成某种破坏。

但是,您可以强制叠加层将自己bottomtop与书籍布局对齐:

android:layout_alignBottom="@id/llBook"
android:layout_alignTop="@id/llBook"

之后可能还有一些边距需要修复,但上面的两行会使叠加层与书籍布局一样大。

答案 2 :(得分:0)

它占据了其父级的全部高度。摆脱孩子线性布局的填充。然后,如果这不起作用。将linearLayout切换为relativeLayout。 (无论如何它更好)另一个限制是你正在使用的实际图像。他们有不同的高度吗?

答案 3 :(得分:0)

完全删除LinearLayout,使用带有src'tick'的imageview作为Relative布局的直接子项:

andriod:centerInParent=true

然后添加具有所需背景透明度的视图

android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"