Android:列表元素,固定布局

标签 android listview android-layout listviewitem

我遇到布局问题。奇怪的是,我在网上找不到解决方案。也许这里有人愿意帮助我? 我想显示一个这样的列表:

enter image description here

但我能得到的只是这个:

enter image description here

这是我的列表项的 xml 代码:

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

    <ImageView android:id="@android:id/icon" android:layout_width="22px"
        android:layout_height="22px" android:layout_marginLeft="4px"
        android:layout_marginRight="10px" android:layout_marginTop="4px"
        android:src="@drawable/icon" />

    <TextView android:id="@android:id/text1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_marginLeft="4px"
        android:layout_marginRight="10px" android:textSize="20px" />

    <TextView android:id="@android:id/text2" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_marginLeft="4px"
        android:layout_marginRight="10px" android:textSize="20px" 
        android:layout_alignParentRight="true" 
        android:layout_weight="0.4" />

</LinearLayout>

非常感谢您的帮助,谢谢;)

最佳答案

在上面的代码中使用RelativeLayout而不是线性布局,因为android:layout_alignParentRight="true"仅适用于relativelatout而不是线性布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content">

    <ImageView android:id="@android:id/icon" android:layout_width="22px"
        android:layout_height="22px" android:layout_marginLeft="4px"
        android:layout_marginRight="10px" android:layout_marginTop="4px"
        android:src="@drawable/icon" />

    <TextView android:id="@android:id/text1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_marginLeft="4px"
        android:layout_marginRight="10px" android:textSize="20px" />

    <TextView android:id="@android:id/text2" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_marginLeft="4px"
        android:layout_marginRight="10px" android:textSize="20px" 
        android:layout_alignParentRight="true" 
        android:layout_weight="0.4"
        />

</RelativeLayout>

关于Android:列表元素,固定布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8286541/

相关文章:

Android Realm ,应用程序卸载

android - 如何在android中创建一个透明的滚动条?

android - 在打开应用程序时恢复 ViewPager 自动滑动

android-layout - 如何在屏幕上拉伸(stretch)三张图像并保持纵横比?

android - 当我尝试使用 ImageReader 获取每个 Camera2 帧时,我得到 "Image is already closed"

android - Horizo​​ntalScrollView 只考虑嵌套 ListView 的第一个 child

JavaFX ListView : retrieve position of a row

android - 如何隐藏android listview中每个项目底部的水平线?

android - 将结束与元素开始相对布局对齐

java - Android TextView 和 ProgressBar 未显示在 LinearLayout 中