【问题标题】:How to create a LinearLayout with TextView and Image on each row to fill up screen height如何在每行上创建一个带有 TextView 和 Image 的 LinearLayout 以填充屏幕高度
【发布时间】:2014-01-09 15:03:10
【问题描述】:

我正在尝试在我的 Android 应用程序中实现以下布局。

我通过使用自定义列表适配器实现了这种布局。但问题在于底部的空白区域。是否可以使 ListView 项目均匀分布以占据整个屏幕高度。

另一种方法是使用带有 WeightSum 的 LinearLayout。但是使用这种方法,我如何能够将 textView 和箭头图像放在同一行中。

任何实现此布局和均匀分布行高的建议都将不胜感激。

提前谢谢你。

主要活动 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/startscreen_background"
    tools:context=".StartScreen" >

    <ListView
        android:id="@+id/start_lv_menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:divider="@color/startscreen_listitem_divider"
        android:dividerHeight="0px" >
    </ListView>

</RelativeLayout>

列表项布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

        <TextView
        android:id="@+id/startscreen_tv_item"
        android:layout_width="match_parent"
        android:layout_height="@dimen/startscreen_listitem_height"
        android:gravity="center_vertical"
        android:paddingLeft="@dimen/startscreen_listitem_paddingleft"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/startscreen_listitem_textcolor"
        android:textSize="@dimen/startscreen_listitem_textsize" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2px"
        android:background="@color/startscreen_listitem_divider" />

</LinearLayout>

【问题讨论】:

  • 您希望每一行都垂直扩展,以使整个视图适合 ListView 的高度?
  • @ramaral:我已经用布局更新了问题..
  • 看到这个answer
  • @ramaral:但我想在 XML 布局中实现这一点,而不是在类中。请提供对此类示例的任何参考。到目前为止,感谢您的帮助.. :-)跨度>
  • @RickFalck:无论哪种方式都可以......我更喜欢基于代码的 XML 布局......

标签: android listview android-linearlayout


【解决方案1】:

您可以在 Main Activity XML 中使用android:layout_height="fill_parent"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/startscreen_background"
tools:context=".StartScreen" >

    <ListView
        android:id="@+id/start_lv_menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:divider="@color/startscreen_listitem_divider"
        android:dividerHeight="0px" >
    </ListView>
</RelativeLayout>

【讨论】:

    【解决方案2】:

    您是否尝试将layout_height="match_parent" 提供给您的 ListView ?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 2015-06-16
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多