【问题标题】:Align a layout at the bottom of another在另一个底部对齐布局
【发布时间】:2015-04-13 14:20:47
【问题描述】:

有没有办法可以在 LinearLayout 的底部对齐 GridView?我想根据上面的布局定位(marginTop)GridView,而不是屏幕顶部。下面是我的代码。谢谢

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@drawable/app_background">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">
    <ImageView
        android:id="@+id/begin_tour"
        android:layout_width="fill_parent"
        android:layout_height="140dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/begin_tour_i"
        android:scaleType="centerCrop" />
 </LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:layout_marginTop="160dp" >
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <GridView
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:verticalSpacing="0dp"
            android:horizontalSpacing="0dp"
            android:stretchMode="columnWidth"
            android:numColumns="2"
            android:background="@drawable/grid_border"/>

    </FrameLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="true"
    android:orientation="vertical"
    android:layout_marginTop="0dp"
    android:layout_alignParentBottom="true" >

    <TextView
        android:id="@+id/link_to_page_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textColor="#000000"
        android:textStyle="bold"
        android:textSize="20dip"
        android:textIsSelectable="true" />
</LinearLayout>

</RelativeLayout>

【问题讨论】:

    标签: android margins


    【解决方案1】:

    只是做一个相对的布局。如果您在TextView 上方需要它,那么只需执行android:layout_above。确保它使用 id,然后输入TextView 的 id。这应该把它放在TextView 的正上方。如果您需要边距,只需添加一个。

    【讨论】:

    • 哦,是的,应该可以。谢谢格兰特。完全忘记了我的想法。
    • @Heidi 你介意接受我的回答吗?我有点需要声誉
    【解决方案2】:

    我认为你应该使用相对布局。

    如果您有一个填满整个屏幕的相对布局,您应该能够使用android:layout_alignParentTopGridview 移动到布局的顶部。

    编辑: 试试这个layout_above

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="@drawable/app_background">
    
        <LinearLayout
             android:orientation="vertical"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_marginTop="10dp">
    
            <ImageView
                android:id="@+id/begin_tour"
                android:layout_width="fill_parent"
                android:layout_height="140dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/begin_tour_i"
                android:scaleType="centerCrop" />
    
        </LinearLayout>
    
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:baselineAligned="false"
            // HERE 
            android:layout_alignParentTop="true"
            android:layout_above="@+id/botlayout"
            android:layout_marginTop="160dp" >
    
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <GridView
                    android:id="@+id/gridview"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:verticalSpacing="0dp"
                    android:horizontalSpacing="0dp"
                    android:stretchMode="columnWidth"
                    android:numColumns="2"
                    android:background="@drawable/grid_border"/>
    
            </FrameLayout>
        </RelativeLayout>
    
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            // HERE
            android:id="@+id/botlayout"
            android:baselineAligned="true"
            android:layout_marginTop="0dp"
            android:layout_alignParentBottom="true" >
    
            <TextView
                android:id="@+id/link_to_page_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:ellipsize="marquee"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:marqueeRepeatLimit="marquee_forever"
                android:scrollHorizontally="true"
                android:singleLine="true"
                android:textColor="#000000"
                android:textStyle="bold"
                android:textSize="20dip"
                android:textIsSelectable="true" />
    
        </RelativeLayout>
    
    </RelativeLayout>
    

    【讨论】:

    • 最后一个LinearLayout也是android:layout_alignParentBottom。会把GridView放在最后一个LinearLayout上面吗?
    • 哦,对不起,它是 alignParentTop,但我认为它应该可以工作。
    • 谢谢马克苏耶。我尝试了您的解决方案,但就像我担心它会与底部的 textview 重叠一样,它确实做到了。我想要它在 LinearLayout 的正下方和 TextView 的正上方。
    猜你喜欢
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 2016-01-07
    • 2017-02-15
    • 2014-01-02
    相关资源
    最近更新 更多