【问题标题】:Inflating List view with Linear Layout, text view not getting match parent使用线性布局膨胀列表视图,文本视图没有匹配父级
【发布时间】:2016-03-08 10:04:18
【问题描述】:

我有一个列表视图,它是一个包含六个文本视图的线性布局。我希望膨胀文本视图的宽度与父视图匹配,但它的宽度是根据我从数据库获得的值设置的(正在包装)。我在线性布局中给 textview 赋予了相同的权重,但是所有 textview 的宽度都不同。文本视图的值来自使用 JDBC 连接的数据库。

列表视图布局为:

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <ListView
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:divider="#0dcd6600"
        android:dividerHeight="2dp" >
    </ListView>

</HorizontalScrollView>

在列表视图中膨胀的行是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:background="#ffffff"
    android:orientation="horizontal"
    android:weightSum="6" >

    <TextView
        android:id="@+id/txt_date"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/border1"
        android:padding="7dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/txt_sname"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/border1"
        android:padding="7dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/txt_standard"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/border1"
        android:padding="7dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/txt_division"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/border1"
        android:padding="7dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/txt_grno"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/border1"
        android:padding="7dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/txt_rollno"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/border1"
        android:padding="7dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

</LinearLayout>

到目前为止,我已经尝试了以下所有选项,但没有任何效果。

  1. 改变 view = inflater.inflate(R.layout.list_item_attendance,null);

    view = inflater.inflate(R.layout.list_item_attendance, parent, false);
  2. 使列表视图的宽度匹配父项
  3. 为所有文本视图制作六个线性布局并使其与父视图匹配。

这是布局不正确的image。文本字段未对齐。

而我想要的就像this

【问题讨论】:

  • 尝试使用表格布局
  • 尽量使用Table Layout,让Textview的宽度平分。
  • 但是列表视图的相同布局正在其他活动中用于查看报告,我有大约 15 个活动我无法在一天内更改它..我有截止日期..任何其他选项合适吗? @Arvindlal Jaiswal
  • 我对这部分感到困惑 -- 我希望膨胀文本视图的宽度与父视图匹配,但它的宽度是根据其中设置的值设置的。 你的意思是,TextViews 的宽度应该足够长,等于父级?还是您的意思是每个 TextView 的宽度都相同? -- 这样它仍然可以水平滚动?
  • 如果你没有为你的文本视图设置任何文本。显示正确吗

标签: android listview android-linearlayout


【解决方案1】:

这是由于 Horizo​​ntalScrollView 中的列表视图而发生的。如果列表视图处于水平滚动视图中,Weightsum 和 weight 不起作用..

【讨论】:

  • 但是如果没有水平滚动,报告将无法正常查看。难道我们没有其他选择可以让它完美吗? @Ashlesha Sharma
  • 回收站视图非常适合您的问题,试试吧!
  • 在尝试了我所知道的所有解决方案之后,我发现如果我们在水平滚动视图中设置了列表视图,那么 weightsum 和 weights 不起作用。删除它会给所有文本视图带来同等的权重。
  • 我找不到仅使用此方法使其对齐的解决方案。但至少现在我知道其中的问题。所以你的答案是正确的。谢谢。@Ashlesha Sharma
【解决方案2】:

这不是解决方案,但我认为您在这里错了。不要使用滚动视图作为父级。请尝试一次。希望它会起作用。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff" >

        <ListView
            android:id="@+id/lv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >
        </ListView>
    </HorizontalScrollView>

</LinearLayout>

【讨论】:

  • 我之前有过这个,但因为我觉得这种相对或线性布局会是问题,所以将其删除。它不起作用..
猜你喜欢
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多