layout_below在linearlayout中

问题描述 投票:0回答:5

是否可以将视图与线性布局中的另一个视图对齐?

我想做这样的事情:http://i.imgur.com/LWgtBKO.png?1

这是否可以通过线性布局实现?

我也想在不使用固定值(如固定高度/宽度)的情况下执行此操作,并且这些视图应该平均填充屏幕(如示例中所示)。

提前致谢

android android-layout android-linearlayout
5个回答
4
投票

Linearlayout将孩子放在垂直或水平。在链接中有imageview textview和表格布局,所以相对布局是更好的解决方案。您可以使用linearlayout来执行此操作。

使用两个linearlayout layoutOne和layoutTwo。

在layoutTwo中放置方向垂直并放置textview和tablelayout。

在layoutOne中将方向水平放置并放置imageview和layoutTwo。

通过这种方式你可以实现它。

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

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:color/background_dark"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="6dp"
            android:src="@drawable/ic_launcher" />

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

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="20dp"
                android:text="TextView" />

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

                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@android:color/background_light"
                        android:text="tablelayout" />
                </TableRow>
            </TableLayout>
        </LinearLayout>
    </LinearLayout>

    <!-- second part -->

    <LinearLayout
        android:layout_width="0dp"
        android:layout_marginLeft="10dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:color/background_light"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="6dp"
            android:src="@drawable/ic_launcher" />

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

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="20dp"
                android:text="TextView" />

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

                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@android:color/background_light"
                        android:text="tablelayout" />
                </TableRow>
            </TableLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>


1
投票

不,你不能在线性布局中使用layout_below

如果您想将物品放在另一个下面,请使用android:orientation="vertical"

如果你想让它们并排放置,那么使用android:orientation="horizontal"


0
投票

要将视图对齐LinearLayout中的另一个视图,您只需将LinearLayout的方向设置为Vertical

android:orientation="vertical"

视图应该平均填充屏幕

要做到这一点,你可以使用match_parent和视图的widthheight属性。


0
投票

当然这是可能的,但你会更好的与RelativeLayout。这将使其更灵活,编码更少,嵌套Layouts更少。你可以有一个LinearLayouthorizontal orientation作为根layout和2 RelativeLayouts内。它基本上就像是

<RelativeLayout
...>
    <ImageView
         .../>
    <TextView
        ...
        android:layout_toRightOf="@+id/imagevViewID"
        android:layout_alignParentTop="true"
    .../>
    <TableLayout
        android:layout_below="@+id/textviewID"
        android:layout_toRightOf="@+id/imageViewID"
    .../>
</RelativeLayout>

我不打算为你写整个Layout,但这样的事情会让你开始。当然,你需要添加widthheight等属性......

看看RelativeLayout Docs你需要的确切属性,但在我看来,RelativeLayout在很多情况下要好得多


0
投票

是的,这是可能的,但为什么你必须使用线性布局?

这是具有线性布局的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2" >

    <!-- MAIN CONTAINER -->

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <!-- FIRST CONTAINER -->

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

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <!-- CHILD OF FIRST CONTAINER -->

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

            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
            </TableLayout>
        </LinearLayout>
    </LinearLayout>

        <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <!-- SECOND CONTAINER -->

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

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <!-- CHILD OF SECOND CONTAINER -->

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

            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
            </TableLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

这是RelativeLayout的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView1"
        android:layout_toRightOf="@+id/imageView1"
        android:layout_alignTop="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TableLayout
        android:layout_toRightOf="@+id/imageView1"
        android:layout_below="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </TableLayout>

    <ImageView
        android:id="@+id/imageView2"
        android:layout_toRightOf="@+id/textView1"
        android:layout_alignTop="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView2"
        android:layout_toRightOf="@+id/imageView2"
        android:layout_alignTop="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TableLayout
        android:layout_toRightOf="@+id/imageView2"
        android:layout_below="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </TableLayout>

</RelativeLayout>

我不是100%确定我的代码将100%显示为您在问题中提供的图片,但我确信您可以从我的代码中获得如何创建这样的布局的全貌。如果你想使用线性布局只是因为你刚刚学习了android并且没有学过相关布局,我认为现在是学习它的最佳时间,因为你可以看到相对布局中的代码比线性布局简单得多。我希望我的回答可以帮助你,如果你有其他问题,请随时在评论中提问:)

© www.soinside.com 2019 - 2024. All rights reserved.