簡體   English   中英

如何將listview行拆分為三列,將其中一列拆分為android中的兩列

[英]how to split listview row into three columns and one of the column into vertically two in android

我需要在列表視圖中列出聯系人以顯示每個行項目的圖像,聯系人姓名,號碼和呼叫圖標。

每行應分成三列。 第一列將具有聯系人圖像,第二列將垂直分為兩個,第一列為聯系人姓名,第二列為數字。 最后一列將有一個圖標/圖像。

所需的布局

我試過下面。 但沒有獲得所需的格式。 這可能與Listview有關,因為我們在下面討論(而不是gridview)?

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

    <ListView
        android:id="@+id/listView_all_cont"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#a556f4"
        android:scrollbars="horizontal"/>


    <ImageView
        android:id="@+id/imgView_contImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:src="@drawable/no_image" />

    <TextView
        android:id="@+id/txtView_name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:textColor="#ffffff"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txtView_number"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:textColor="#ffffff"
        android:textSize="14sp" />

</LinearLayout>

有人可以幫忙嗎?

是的,可以使用ListView 我建議你了解RecyclerView RecyclerView提供更多而不是ListView 您只需創建一個item_row.xml並在Adapter為該行項目充氣。 試試這個代碼。

item.row.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="105dp">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".3">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".7"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="Text1"
                android:paddingLeft="10dp"
                android:gravity="center_vertical"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="Text1"
                android:paddingLeft="10dp"
                android:gravity="center_vertical"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".2">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

您可以像這樣使用ActivityFragment布局,

main_layout.xml

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

    <ListView
        android:id="@+id/listView_all_cont"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#a556f4"
        android:scrollbars="horizontal"/>


</LinearLayout>

列出項目布局list_item.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="wrap_content"
    android:paddingBottom="8dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="8dp">

    <ImageView
        android:id="@+id/user_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_action_name"/>

    <ImageView
        android:id="@+id/action_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_action_name"/>

    <TextView
        android:id="@+id/name_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/action_image"
        android:layout_toRightOf="@+id/user_image"
        android:padding="5dp"
        android:text="XYZ"/>

    <TextView
        android:id="@+id/number_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name_tv"
        android:layout_toLeftOf="@+id/action_image"
        android:layout_toRightOf="@+id/user_image"
        android:padding="5dp"
        android:text="12345"/>

</RelativeLayout>

您也可以嘗試RecycleView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM