【问题标题】:Android - LinearLayout/RelativeLayout AlignmentAndroid - LinearLayout/RelativeLayout 对齐
【发布时间】:2015-07-20 05:13:25
【问题描述】:

我正在尝试创建一个简单的 UI,看起来像这样

我对 android 很陌生,一直在尝试不同类型的布局(线性/相对),我相信线性可能是这里的方法(我希望在某个阶段使 ListViews 的数量动态,但现在 2没问题)。

我应该使用线性布局还是相对布局,或者两者的某种组合来实现这一点。

这是我拥有的 XML,我似乎无法让按钮正确对齐,即使它的重力设置为正确。我愿意接受有关如何解决此问题的任何建议,或者是否有更好的方法

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9"

    android:orientation="horizontal" >

    <ListView
        android:id="@+id/lv1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        />

    <ListView
        android:id="@+id/lv2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    android:orientation="horizontal" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="New Button"
        android:id="@+id/button"
        android:gravity="right" />
</LinearLayout>

编辑:这是 UI 当前的样子

【问题讨论】:

  • 以相对布局为主。然后采用线性布局,在其中放置两个列表视图。然后在主相对布局上添加按钮,并设置 align parent right true 和线性布局下方。
  • 在这种情况下如何避免布局嵌套,如果我想让 listView1 30% 的宽度和 listView2 50% 的宽度,考虑到我现在正在使用 @sanatshukla 的答案,我真的不看看我们如何在没有布局嵌套的情况下做到这一点。
  • 我发布了一个答案,向您展示如何充分利用 RelativeLayout 来避免布局嵌套。但我的 ListViews 使用 50% 和 50%。您的问题中没有证据表明您想要 30% 和 50%。甚至在您的绘图中也没有。
  • 你是对的,问题中没有证据,但我只是想知道如何扩展所有给出的答案
  • 请检查我的答案,如果它不适合你,我会删除它。

标签: android xml user-interface android-linearlayout android-relativelayout


【解决方案1】:

好的,你可以做一件事。

<RelativeLayout>
<LinearLayout> <!-- horizontal orientation with weightsum 2 -->

<LinearLayout>  <!-- vertical orientation with layout weight 1 and width 0dp --> 
</LinearLayout>

<LinearLayout>  <!-- vertical orientation with layout weight 1 and width 0dp -->
</LinearLayout>

</LinearLayout>  

<Button /> <!-- alignParentBottom = true and alignParentLeft = true --> 
</RelativeLayout>

【讨论】:

    【解决方案2】:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_marginTop="18dp"
            android:layout_above="@+id/btnButton"
            android:weightSum="2"
            android:orientation="horizontal" >
    
            <ListView 
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1">
    
            </ListView>
            <ListView 
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1">
    
            </ListView>
        </LinearLayout>
    
        <Button 
            android:id="@+id/btnButton"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Button"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"/>
    
    </RelativeLayout>
    

    【讨论】:

    • 请注意,不建议将 ListView 的高度设置为 wrap_content ,我猜在这种情况下它们应该是 match_parent 。来源:youtube.com/watch?v=wDBM6wVEO70&t=40m50s
    • @suphug22 如果此答案有帮助,请接受它作为正确答案。
    【解决方案3】:

    只需移除 Button 周围的 LinearLayout 或将其方向设置为垂直即可。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="1">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.9"
            android:orientation="horizontal" >
    
            <ListView
                android:id="@+id/lv1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1.0"/>
    
            <ListView
                android:id="@+id/lv2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1.0"/>
    
        </LinearLayout>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="New Button"
            android:id="@+id/button"
            android:gravity="right" />
    </LinearLayout>
    

    【讨论】:

    • 这样也达到了预期的效果
    【解决方案4】:

    避免布局嵌套
    单个 RelativeLayout 就足够了。

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    
        <!-- Set the Button, first: Bottom and right aligned -->
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:text="New Button"
        />
    
        <!-- Trick: set a dummy View in the middle of the screen -->
        <View
            android:id="@+id/dummy"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_centerInParent="true"
        />
    
        <!-- Now the ListViews: -->
        <!-- One above the button and to the left of the dummy -->
        <ListView
            android:id="@+id/lv1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/button"
            android:layout_toLeftOf="@id/dummy"
        />
    
        <!-- One above the button and to the right of the dummy -->
        <ListView
            android:id="@+id/lv2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/button"
            android:layout_toRightOf="@id/dummy"
        />
    </RelativeLayout>
    

    【讨论】:

    • 酷,我如何扩展它以在原始 2 的右侧添加另一个 ListView 并让它们各自占据屏幕的三分之一,我尝试添加另一个只是 toRightOf lv2 但是它似乎在角落编辑中被压扁:只是试图更好地理解总体布局,而不是完全针对问题
    • 您要求 2 个。要设置 3 个等间距的,您必须使用带有权重的 LinearLayout。但。由于 Button,您将需要另一个布局。这打破了布局嵌套避免。
    • 再一次你是对的,我只想要两个,很抱歉有任何混淆,但就像我在上面的评论中提到的那样,我只是想知道如何扩展答案。感谢您为避免布局嵌套的问题提供替代方案!
    【解决方案5】:

    我觉得你可以试试这个方法....

    <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_gravity="right" android:layout_weight="0.1" android:gravity="right" android:orientation="horizontal" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" /> </LinearLayout>
    

    【讨论】:

    • 这肯定也行,但是为什么我们需要放置一个不可见的按钮,看起来有点奇怪的对齐方法
    猜你喜欢
    • 2023-03-18
    • 2011-08-19
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多