【问题标题】:layout_below in custom adapter in listviewlistview 中自定义适配器中的 layout_below
【发布时间】:2017-06-01 05:31:05
【问题描述】:

我为listview 定制了一个adapter,但它不起作用,两个textview 在我构建时覆盖,但是当我制作xml 时它起作用了,我使用了layout_below

字符串“nguyen quoc bao”和“13530”叠加
在xml中

我的xml 代码:

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

    <TextView
        android:id="@+id/textView_stud_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:paddingLeft="8dp"
        android:textSize="18sp"
        android:textStyle="bold"
        android:text="111"/>

    <TextView
        android:id="@+id/textView_stud_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_below="@+id/textView_stud_name"
        android:paddingLeft="8dp"
        android:textSize="12sp"
        android:text="111"/>

    <Button
        android:id="@+id/btn_absence"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:background="@color/red"
        android:text="Absence" />

    <Button
        android:id="@+id/btn_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/btn_absence"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:background="@color/green"
        android:text="Check" />

</RelativeLayout>

如何解决这个问题?感谢回复。

【问题讨论】:

    标签: android listview android-adapter


    【解决方案1】:

    按原样粘贴此布局,这种设计为您将来的更改提供了更大的灵活性(提示 - 尽可能使用线性布局,因为相对布局可能会在运行时过多地改变他的行为)

     <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <LinearLayout
                android:layout_alignParentLeft="true"
                android:layout_width="match_parent"
                android:orientation="vertical"
                android:layout_toLeftOf="@+id/llButtons"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/textView_stud_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:paddingLeft="8dp"
                    android:text="111"
                    android:textSize="18sp"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/textView_stud_id"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/textView_stud_name"
                    android:layout_centerVertical="true"
                    android:paddingLeft="8dp"
                    android:text="111"
                    android:textSize="12sp" />
    
            </LinearLayout>   
    
            <LinearLayout
                android:id="@+id/llButtons"
                android:layout_alignParentRight="true"
                android:layout_width="wrap_content"
                android:orientation="horizontal"
                android:layout_height="wrap_content">
                <Button
                    android:id="@+id/btn_absence"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:textColor="@color/black"
                    android:layout_marginRight="5dp"
                    android:background="@color/red"
                    android:text="Absence" />
    
                <Button
                    android:id="@+id/btn_check"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="5dp"
                    android:layout_toLeftOf="@id/btn_absence"
                    android:background="@color/green"
                    android:text="Check" />
    
            </LinearLayout>
    
    
        </RelativeLayout>
    

    【讨论】:

    • 感谢帮助,但是如果我想继续我的第一种方式,如何解决?
    • 你必须保持太多的视图关系.....像这个视图左边这个视图这个视图右边这个视图下面这个上面这个......那个变得更加忙碌并且无法保持他的形状
    【解决方案2】:

    从第二个 TextView 中删除 layout_centerVertical 属性。

    如果为 true,则在其父级中垂直居中此子级。

     <TextView
            android:id="@+id/textView_stud_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:paddingLeft="8dp"
            android:textSize="18sp"
            android:textStyle="bold"
            android:text="111"/>
    
            <TextView
                android:id="@+id/textView_stud_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textView_stud_name"
                android:paddingLeft="8dp"
                android:textSize="12sp"
                android:text="111"/>
    

    【讨论】:

    • 它移到下面但仍然重叠,我认为是因为边距/填充
    • @HoàngĐăng 更好的方法,使用 LinearLayout 并添加 android:layout_weight
    • @HoàngĐăng 如果您遇到问题,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    相关资源
    最近更新 更多