有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么我的线性布局与listview中的父级高度不匹配?

我有以下代码

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:id="@+id/itemLayoutView"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:paddingLeft="@dimen/list_view_layout_margin"
    安卓:paddingRight="@dimen/list_view_layout_margin" 
    安卓:background="@color/yellow">

    <LinearLayout
        安卓:id="@+id/itemLinearLayoutView"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:orientation="vertical" >

        <TextView
            安卓:id="@+id/gameNameView"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:textSize="@dimen/game_list_text_size" />

        <TextView
            安卓:id="@+id/gameCreationDateView"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:textColor="@color/gray"
            安卓:textSize="@dimen/small_text_size"
            安卓:textStyle="italic" />
    </LinearLayout>

    <LinearLayout
        安卓:id="@+id/LinearLayoutView"
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:background="@color/red"
        安卓:orientation="horizontal" >

        <Button
            安卓:id="@+id/infoButton"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="@string/info" />

        <Button
            安卓:id="@+id/deleteButton"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="@string/delete" />
    </LinearLayout>


</RelativeLayout>

id为itemLinearLayoutView的LinearLayout是将所有内容包装在listview行中的主布局。我遇到的问题是,第二个线性布局子级不采用id为itemLinearLayoutView的父线性布局的高度。有什么解决办法吗?还是代码有问题


共 (5) 个答案

  1. # 1 楼答案

    <LinearLayout
        android:id="@+id/itemLinearLayoutView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" <!-- you should use match_parent here -->
        android:orientation="vertical" >
    
  2. # 2 楼答案

    只需更改布局高度以匹配父级。问题会解决的

      <LinearLayout
        android:id="@+id/itemLinearLayoutView"
        android:layout_width="match_parent"
      // use match_parent instead of wrap_content 
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
  3. # 3 楼答案

    布局需要嵌套在第一个线性布局中,以获取其参数,并且布局高度的值需要更改。代码如下

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/itemLayoutView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/list_view_layout_margin"
    android:paddingRight="@dimen/list_view_layout_margin" 
    android:background="@color/yellow">
    
    <LinearLayout
        android:id="@+id/itemLinearLayoutView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/gameNameView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/game_list_text_size" />
    
        <TextView
            android:id="@+id/gameCreationDateView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/gray"
            android:textSize="@dimen/small_text_size"
            android:textStyle="italic" />
    
    
    <LinearLayout
        android:id="@+id/LinearLayoutView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/red"
        android:orientation="horizontal" >
    
        <Button
            android:id="@+id/infoButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/info" />
    
        <Button
            android:id="@+id/deleteButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/delete" />
    
    </LinearLayout>
    
    </LinearLayout>
    

  4. # 4 楼答案

    // try this way i have used LinearLayout as parent rather than RelativeLayout
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/itemLayoutView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingLeft="@dimen/list_view_layout_margin"
        android:paddingRight="@dimen/list_view_layout_margin"
        android:background="@color/yellow">
    
        <LinearLayout
            android:id="@+id/itemLinearLayoutView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <TextView
                android:id="@+id/gameNameView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="@dimen/game_list_text_size" />
    
            <TextView
                android:id="@+id/gameCreationDateView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/gray"
                android:textSize="@dimen/small_text_size"
                android:textStyle="italic" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/LinearLayoutView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="right"
            android:background="@color/red"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/infoButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/info" />
    
            <Button
                android:id="@+id/deleteButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/delete" />
        </LinearLayout>
    </LinearLayout>
    
  5. # 5 楼答案

    你的问题在于:

    android:layout_height="wrap_content"
    

    在线性布局中,需要将其更改为“匹配父对象”,以作为父对象的大小。还有,你说

    the second linear layout child does not take the height of the parent linear layout
    

    这两种布局互不相关。父母是你的亲戚,你的两个直系亲属都是这个亲戚的孩子。也许这将帮助你调试你遇到的任何进一步的问题