7

我正在尝试在 android studio 中重新创建以下布局:

预览图像

因为我是 android 的新手,所以我首先尝试了 LinearLayout,并认为这可能是不可能的。现在我正在尝试使用 RelativeLayout 我已经用颜色创建了这个块:

        <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="80dp"
                android:background="@color/red_top">
        </RelativeLayout>

现在我想问我如何划分它,就像它在相同布局中显示在顶部的图像 1 条和底部的 2 条一样,我怎样才能放置相同的边框?

4

5 回答 5

20

您可以使用以下 xml 代码来执行此操作:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/custom_toast_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#CD96CD"
        android:text="TEXT" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#CD96CD"
            android:text="TEXT" />

        <View
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:background="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#CD96CD"
            android:text="TEXT" />
    </LinearLayout>

</LinearLayout>
于 2013-08-26T13:17:07.820 回答
1
<?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="match_parent" >

<LinearLayout 
    android:id="@+id/FirstLinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/FirstTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEXT_ONE" />
</LinearLayout>
<LinearLayout
    android:id="@+id/SecondLinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/FirstLinearLayout"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/SecondTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEXT_TWO" />

    <TextView
        android:id="@+id/ThirdTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEXT_Three" />
</LinearLayout>

首先,您需要一个容器来容纳所有组件;在我的示例中,我正在谈论的容器是RelativeLayout. 相对布局允许他们的孩子使用相应的 ID 放置在他们的位置。看看我是如何LinearLayout使用android:layout_below="@+id/FirstLinearLayout".

如果您真的坚持将它们放在同一个 Layout 中,请使用相对布局并将TextViews 定位如下:

<?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="match_parent" >


    <TextView
        android:id="@+id/FirstTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEXT_ONE" />


    <TextView
        android:id="@+id/SecondTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/FirstTextView"
        android:text="TEXT_TWO" />

    <TextView
        android:id="@+id/ThirdTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/FirstTextView"
        android:layout_toRightOf="@+id/SecondTextView"
        android:text="TEXT_Three" />


</RelativeLayout>

我希望这能够帮到你。

于 2013-08-26T13:18:13.657 回答
1

ATableLayout是满足您需求的最佳选择。

<TableLayout>
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text="Text" />

    </TableRow>
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text" />

    </TableRow>
</TableLayout>

请注意第一个跨越两列的layout_span属性。TextView

于 2013-08-26T13:18:52.780 回答
1

你可以试试这个来添加额外的 TextViews

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#CD96CD"
    android:text="TEXT" />

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000000" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#CD96CD"
        android:text="TEXT" />
     <View
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:background="#000000" />
     <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#CD96CD"
        android:text="TEXT" />

    <View
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:background="#000000" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#CD96CD"
        android:text="TEXT" />
     <View
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:background="#000000" />
     <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#CD96CD"
        android:text="TEXT" />
</LinearLayout>

于 2013-08-27T03:51:20.193 回答
0

添加此视图;在你的文本视图之间画一个分隔符

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000000" />
于 2016-12-19T14:21:04.530 回答