【问题标题】:How to implement this kind of layout如何实现这种布局
【发布时间】:2013-11-16 01:41:48
【问题描述】:

这是design effect

我尝试使用LinearLayout 来实现它:

    <LinearLayout
        android:clickable="true"
        android:focusable="true"
        android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:gravity="center">

        <TextView
            android:text="Type"
            android:background="@drawable/poi_nearby_cell"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:layout_weight="1"/>

        <LinearLayout
            android:orientation="vertical"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_weight="1">

            <TextView
                android:text="Type1"
                android:background="@drawable/poi_nearby_cell"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"/>

            <TextView
                android:text="Type2"
                android:background="@drawable/poi_nearby_cell"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"/>
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_weight="1">

            <TextView
                android:text="Type3"
                android:background="@drawable/poi_nearby_cell"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"/>

            <TextView
                android:text="Type4"
                android:background="@drawable/poi_nearby_cell"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

还有poi_nearby_cell

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid
        android:color="@android:color/transparent">
    </solid>

    <stroke
        android:width="0.5dp"
        android:color="#A0A0A0">
    </stroke>

</shape>

This 是我现在得到的。

但是我遇到了很多问题:

1 我使用drawable/poi_nearby_cell 为文本视图创建边框。

但是我还必须实现像设计效果图中的“Type1”这样的单元格按下效果。通常我将 axml 可绘制资源设置为这样的背景:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <color android:color="#ffebf6ff"/>
    </item>
    <item>
        <color android:color="#fffefefe"/>
    </item>
</selector>

但就我而言,我无法同时设置具有两个背景的视图。有什么选择吗?

2 如您所见,边框具有不同的宽度值。通常我希望边框为1dp,但在我的示例中,两个单元格之间的边框可能为2dp

我不知道继续。有什么建议吗?

【问题讨论】:

  • 我试过表格布局,但不支持行跨度。
  • 在表中使用表:外部表有 2 列和 1 行。右侧面板在 2 列和 3 行内。
  • 那么cell边框和cell-press样式呢?
  • @hguser 它支持spans!
  • @rekire:stackoverflow.com/a/16126378/306719。检查这个。

标签: android android-layout


【解决方案1】:
// try this
**layout_bg.xml**
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#333333" />

    <corners android:radius="5dp" />

    <stroke
        android:width="1dp"
        android:color="@android:color/darker_gray" />

</shape>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:clickable="true"
    android:focusable="true"
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:padding="5dp"
    android:gravity="center">

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:background="@drawable/layout_bg"
        android:padding="1dp">

        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:padding="1dp"
            android:background="@android:color/darker_gray"
            android:layout_weight="1">
            <LinearLayout
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:background="@android:color/white"
                android:orientation="vertical">
                <TextView
                    android:text="Type"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"/>
            </LinearLayout>
        </LinearLayout>


        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:orientation="vertical"
            android:layout_weight="1">
            <LinearLayout
                android:layout_height="0dp"
                android:layout_weight="1"
                android:padding="1dp"
                android:layout_width="match_parent"
                android:background="@android:color/darker_gray"
                android:orientation="vertical">
                <LinearLayout
                    android:layout_height="match_parent"
                    android:layout_width="match_parent"
                    android:gravity="center"
                    android:background="@android:color/white">

                    <TextView
                        android:text="Type1"
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"/>
                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_height="0dp"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:background="@android:color/darker_gray"
                android:padding="1dp"
                android:orientation="vertical">
                <LinearLayout
                    android:layout_height="match_parent"
                    android:layout_width="match_parent"
                    android:gravity="center"
                    android:background="@android:color/white">
                    <TextView
                        android:text="Type2"
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"/>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:orientation="vertical"
            android:layout_weight="1">
            <LinearLayout
                android:layout_height="0dp"
                android:layout_weight="1"
                android:padding="1dp"
                android:layout_width="match_parent"
                android:background="@android:color/darker_gray"
                android:orientation="vertical">
                <LinearLayout
                    android:layout_height="match_parent"
                    android:layout_width="match_parent"
                    android:gravity="center"
                    android:background="@android:color/white">

                    <TextView
                        android:text="Type3"
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"/>
                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_height="0dp"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:background="@android:color/darker_gray"
                android:padding="1dp"
                android:orientation="vertical">
                <LinearLayout
                    android:layout_height="match_parent"
                    android:layout_width="match_parent"
                    android:gravity="center"
                    android:background="@android:color/white">
                    <TextView
                        android:text="Type4"
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"/>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多