【问题标题】:Design ListView In Android在 Android 中设计 ListView
【发布时间】:2015-05-23 13:40:40
【问题描述】:

我想像下面给出的这个图片链接一样设计 listView,http://i.stack.imgur.com/M8oQY.png,我尝试了所有类型的布局,但仍然无法像这样定义 listview,请帮助我, 我的代码如下。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#ffff"
android:focusable="false"
android:id="@+id/notifTable"
android:layout_height="fill_parent">

<LinearLayout
  android:layout_height="fill_parent"
  android:layout_width="wrap_content"
  android:background="#08707D"
  android:orientation="vertical"
  android:id="@+id/tableRow1">
<ImageView
    android:layout_height="60dp"
    android:layout_width="60dp"
    android:src="@drawable/alert"
    android:padding="5dp"
    /> 
 </LinearLayout>

<LinearLayout
  android:layout_width="0dp"
  android:layout_height="fill_parent"
  android:background="#08707D"
  android:layout_weight="1"
  android:orientation="vertical"
  android:id="@+id/tableRow2"
>
<TextView
  android:id="@+id/label1"
  android:layout_height="match_parent"
  android:layout_width="fill_parent"
  android:text="@string/hello_world"
  android:singleLine="true"
  android:layout_weight="0.5"
  android:textColor="@color/button"
  style="@style/listTextStyle"
       >
   </TextView>
   <TextView
     android:id="@+id/label2"
     android:layout_height="wrap_content"
     android:layout_width="fill_parent"
      android:text="@string/hello_world"
     android:singleLine="true"
      android:layout_weight="0.5"
      android:textColor="@color/button"
      >
    </TextView>

  </LinearLayout>

 </LinearLayout>

【问题讨论】:

  • 你有这个list_item_view每个的listview
  • 我在我的自定义适配器中添加了这个带有列表视图的布局
  • 那么你是不是没有得到图片中的列表视图或者你什么都没有得到
  • i.stack.imgur.com/UnVJL.png,我是这样看的
  • 问题是您使用的是水平线性布局,并且您已将图像视图的宽度/高度设置为 60dp。然后,您的其他线性布局(用于两个文本)是adopting 这个高度为 60dp 以显示其内容。尝试将 ImageView 的宽度/高度设置为wrap_content,看看这是否会改变事物的显示方式。我还建议使用 RelativeLayout 作为最顶层的布局和 ImageView 的父级而不是线性布局,但这更多是个人喜好和优化技巧。

标签: android android-layout listview android-listview


【解决方案1】:

您需要在布局中添加ListView 控件:

ListView 是一个显示可滚动项目列表的视图组。这 列表项使用适配器自动插入到列表中 从诸如数组或数据库查询之类的源中提取内容 并将每个项目结果转换为放入列表中的视图。

您尚未在布局中添加列表视图,更多参考参考此示例 Custom ListView Android

而且你还需要更新列表项线性布局

<LinearLayout
        android:id="@+id/tableRow2"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#08707D"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/label1"
            style="@style/listTextStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="@string/hello_world"
            android:textColor="@color/button" >
        </TextView>

        <TextView
            android:id="@+id/label2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="@string/hello_world"
            android:textColor="@color/button" >
        </TextView>
    </LinearLayout>

【讨论】:

  • 我在自定义适配器中添加了 ListView
  • 那你遇到了什么问题?
  • 我已经使用RelativeLayout解决了谢谢兄弟的帮助.. :)
【解决方案2】:

试试这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#ffff"
android:focusable="false"
android:id="@+id/notifTable"
android:layout_height="wrap_content">


<ImageView
    android:layout_height="60dp"
    android:layout_width="60dp"
    android:src="@drawable/alert"
    android:padding="5dp"
    /> 

	<LinearLayout
	  android:layout_width="0dp"
	  android:layout_height="wrap_content"
	  android:background="#08707D"
	  android:layout_weight="1"
	  android:orientation="vertical"
	  android:id="@+id/tableRow2"
	>
<TextView
  android:id="@+id/label1"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:text="@string/hello_world"
  android:singleLine="true"
  android:textColor="@color/button"
  style="@style/listTextStyle"
       >
   </TextView>
   <TextView
     android:id="@+id/label2"
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
      android:text="@string/hello_world"
     android:singleLine="true"
      android:textColor="@color/button"
      >
    </TextView>

【讨论】:

    猜你喜欢
    • 2021-09-02
    • 2015-09-05
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多