创建自定义布局/视图

时间:2015-10-22 03:00:36

标签: android android-layout layout view

Sample

我已经为此视图设计了xml布局,但其视图层次结构非常深,例如根布局是包含卡片视图的相对布局。此卡片视图中的根是包含图像的相对布局在右边(泰勒斯威夫特)和底部的文本视图。包含细节的左侧部分是线性布局(垂直),每行依次是包含图标(共享)和文本的线性布局(水平)。 有没有更好的方法来做到这一点,或者我应该使用自定义布局,如果是这样的话?

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

<android.support.v7.widget.CardView
    android:id="@+id/person_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:elevation="2dp"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="2dp"
    card_view:cardUseCompatPadding="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/person_1_image"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignTop="@+id/person_1_image"
            android:layout_toLeftOf="@+id/person_1_image"
            android:layout_toStartOf="@+id/person_1_image"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/call_layout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackground"
                android:clickable="true"
                android:orientation="horizontal"
                android:padding="5dp">

                <ImageView
                    android:id="@+id/call"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_marginLeft="15dp"
                    android:layout_marginStart="15dp"
                    android:contentDescription="@string/call"
                    android:gravity="center"
                    android:src="@drawable/ic_share_black_36dp" />

                <TextView
                    android:id="@+id/number"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="50dp"
                    android:layout_marginStart="50dp"
                    android:gravity="center|start"
                    android:lines="1"
                    android:maxLines="1"
                    android:text="98984968468"
                    android:textSize="17sp" />
            </LinearLayout>

            <View
                android:id="@+id/divider"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginLeft="80dp"
                android:layout_marginStart="80dp"
                android:background="@color/grey" />

            <LinearLayout
                android:id="@+id/email_layout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackground"
                android:clickable="true"
                android:orientation="horizontal"
                android:padding="5dp">

                <ImageView
                    android:id="@+id/email"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_marginLeft="15dp"
                    android:layout_marginStart="15dp"
                    android:contentDescription="@string/email"
                    android:gravity="center"
                    android:src="@drawable/ic_share_black_36dp" />

                <TextView
                    android:id="@+id/email_id"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="50dp"
                    android:layout_marginStart="50dp"
                    android:ellipsize="marquee"
                    android:gravity="center|start"
                    android:lines="1"
                    android:maxLines="1"
                    android:text="abc@gmail.com"
                    android:textSize="17sp" />
            </LinearLayout>

            <View
                android:id="@+id/divider2"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginLeft="80dp"
                android:layout_marginStart="80dp"
                android:background="@color/grey" />

            <LinearLayout
                android:id="@+id/sms_layout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackground"
                android:clickable="true"
                android:orientation="horizontal"
                android:padding="5dp">

                <ImageView
                    android:id="@+id/sms"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:gravity="center"
                    android:src="@drawable/ic_share_black_36dp" />

                <TextView
                    android:id="@+id/sms_num"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="50dp"
                    android:layout_marginStart="50dp"
                    android:ellipsize="marquee"
                    android:gravity="center|start"
                    android:lines="1"
                    android:maxLines="1"
                    android:text="987544244"
                    android:textSize="17sp" />
            </LinearLayout>

        </LinearLayout>

        <ImageView
            android:id="@+id/person_1_image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginEnd="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginTop="5dp"
            android:contentDescription="@string/anniversary_image"
            android:scaleType="fitCenter"
            android:src="@drawable/profile_p" />

        <TextView
            android:id="@+id/person_1_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/person_1_image"
            android:ellipsize="middle"
            android:gravity="center|bottom"
            android:maxLines="1"
            android:padding="5dp"
            android:singleLine="true"
            android:text="User name"
            android:textColor="@color/textColorPrimary"
            android:textSize="20sp"
            android:textStyle="bold" />


    </RelativeLayout>
</android.support.v7.widget.CardView>

1 个答案:

答案 0 :(得分:0)

我认为你可以删除很多布局层次结构。 看起来你应该可以将CardLayout作为根。 然后将RelativeLayout作为CardLayout的唯一子项。 然后,其他一切都是RelativeLayout的直接子项。

您应该能够将所有内容放在RelativeLayout中 使用相对定位,无需借助更多嵌入式布局。

我没看到自定义布局如何帮助,并且假设您从未编写过 之前,我认为这将比它的价值更麻烦。

相关问题