【问题标题】:Create a custom layout/view创建自定义布局/视图
【发布时间】:2016-01-21 05:45:12
【问题描述】:

我已经为这个视图设计了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>

【问题讨论】:

  • 第一次在这个网站看到TS。 . .有很多方法可以......你可以做自定义视图扩展RelativeLayout类......或者只是在每次你需要它时膨胀那个xml......
  • 其实我以前从来没有写过自定义视图,想学习,如果你知道的话,你能提供一些好的网站或地方来学习吗?
  • Android 开发者网站中的 Android 的 Javadocs.. 如果您扩展类,对 View 和 ViewGroup 层次结构有一般的了解,您可以操作 View
  • 根RelativeLayout提供什么?如果卡片视图是根会发生什么?否则,其余的听起来就像我将实现您所展示的方式,除了垂直 LinearLayout 内的 RelativeLayout。
  • 请将您的 xml 布局添加到问题中

标签: android android-layout layout view


【解决方案1】:

我认为您可以删除很多布局层次结构。 看起来您应该能够将 CardLayout 作为根。 然后将 RelativeLayout 作为 CardLayout 的唯一子级。 然后,其他所有内容都作为 RelativeLayout 的直接子级。

您应该能够在 RelativeLayout 中定位所有内容 使用相对定位,而不必求助于更多的嵌入式布局。

我看不出自定义布局有什么帮助,因为你从来没有写过 之前的一个,我认为这将比它的价值更麻烦。

【讨论】:

  • 添加了 xml 布局,您能否展示可以删除哪些元素,同时仍保留最终布局。
猜你喜欢
  • 1970-01-01
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-23
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多