如何将listview行拆分为三列,将其中一列拆分为android中的两列

时间:2017-08-05 08:16:05

标签: android android-layout listview

我需要在列表视图中列出联系人,以显示每个行项目的图片,联系人姓名,号码和呼叫图标。

每行应分为三列。 第一列将具有联系人图像,第二列将垂直分为两个,第一列为联系人姓名,第二列为数字。 最后一列将有一个图标/图像。

required layout

我在下面试过。但没有获得所需的格式。 这可能与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>

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

是的ListView可以。我建议你了解RecyclerViewRecyclerView代替ListView而不是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> 中为该行项目充气。试试这段代码。

<强> item.row.xml

func createBordersWithColor(color: UIColor, myView : UIView) {

    myView.layer.borderWidth = 1
    myView.layer.cornerRadius = 0
    myView.layer.shouldRasterize = false
    myView.layer.rasterizationScale = 2
    myView.clipsToBounds = true
    myView.layer.masksToBounds = true
    let cgColor: CGColor = color.cgColor
    myView.layer.borderColor = cgColor
}

答案 1 :(得分:1)

您可以使用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