簡體   English   中英

Android - 如何使子LinearLayout與父RelativeLayout的高度匹配

[英]Android - How to make child LinearLayout match parent RelativeLayout's height

我將這個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)的寬度,而不是高度。

這是我打算做的: 在此輸入圖像描述

這就是我得到的: 在此輸入圖像描述

LinearLayout沒有占據其父級的整個高度。

有兩個原因,為什么我使用相對布局作為父級:

  1. LinearLayout應該具有40%黑色的書本信息(應該覆蓋)

  2. 在書的頂部顯示此符號

在此輸入圖像描述

整個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中使用了這個,但效果是這樣的:

在此輸入圖像描述

Try3 :正如codeMagic建議的那樣,我改變了孩子的LinearLayout

gravity="center"

到ImageView的

layout_gravity="center"

結果,LinearLayout沒有占據整個高度。

所以我的問題是如何使子LinearLayout匹配父RelativeLayout的高度

嘗試將其添加到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" />

我認為使用這種布局本身很好。 事實上,你將它放入ListView導致某種程度的破壞。

但是,您可以強制疊加層將自己的bottomtop與書籍布局對齊:

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

在那之后,你可能仍然需要修復一些邊距,但上面的兩行將使疊加層與書籍布局一樣大。

它占據了其父母的全部高度。 擺脫孩子線性布局的填充。 然后,如果這不起作用。 將linearLayout切換為relativeLayout。 (無論如何更好)另一個限制是你正在使用的實際圖像。 他們有不同的高度嗎?

完全刪除LinearLayout,使用帶有src'tick'的imageview作為Relative布局的直接子項:

andriod:centerInParent=true

然后添加具有所需背景透明度的視圖

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM