【问题标题】:Layout Help, Disappearing LinearLayouts布局帮助,消失的线性布局
【发布时间】:2011-09-25 15:52:45
【问题描述】:

我正在尝试设计一个简单的布局,在页面底部居中的水平线性布局中包含 2 个按钮(因此 layout_gravity="bottom")。而且我还需要一个位于屏幕中心的垂直线性布局,可以容纳 3 个 TextView(在程序中稍后实现,因此只需要一个 LinearLayout 就可以容纳 3 个 TextView)。

我可以将按钮放在正确的位置(即使我希望它们更加分开),但是每次我尝试使 TextViews LinearLayout 不出现或使另一个消失时。我一直在努力解决这个问题......有人可以帮忙吗?谢谢

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@color/background">
<LinearLayout
    android:id="@+id/game_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="bottom"
    android:layout_margin="100dip">
    <Button
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next_label"
        android:layout_gravity="center"/>
    <Button
        android:id="@+id/repeat_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/repeat_label"
        android:layout_gravity="center"/>   
</LinearLayout>
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:layout_weight="1">
</LinearLayout>
</LinearLayout>

【问题讨论】:

  • 没有一个答案真的对我有用......仍在努力解决

标签: android android-layout interface android-linearlayout


【解决方案1】:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout android:id="@+id/linearLayout2"
        android:layout_width="match_parent" android:orientation="vertical"
        android:layout_height="wrap_content" android:layout_weight="1"
        android:gravity="center">
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout1"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_gravity="bottom" android:gravity="center">
        <Button android:text="Button" android:id="@+id/button1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="Button" android:id="@+id/button2"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    </LinearLayout>
</LinearLayout>

linearlayout2 是垂直布局,子元素位于中心

这是你需要的吗?

带有“Some text”值的EditText通过java添加,同时LayoutParams设置为WRAP_CONTENT

【讨论】:

    【解决方案2】:

    确保 2 个Buttons 的水平LinearLayout 的高度设置为wrap_content。 你也可以取一个RelativeLayout并分别保留两个垂直和水平方向的LinearLayout

    这个代码就足够了:

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/background">
        <LinearLayout
            android:id="@+id/game_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignparentbottom="true"
            android:layout_margin="100dip">
            <Button
                android:id="@+id/next_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/next_label"
                android:layout_gravity="center"/>
            <Button
                android:id="@+id/repeat_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/repeat_label"
                android:layout_gravity="center"/>   
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_centerinparent="true"
            android:layout_weight="1">
        </LinearLayout>
    </RelativeLayout>
    

    警告:属性的大小写可能不同,因此请更正它们。

    【讨论】:

    • 相对布局是什么意思?我有点困惑...我的代码只是让 textview 布局变得一团糟...
    • 像线性布局一样,还有另一个名为相对布局的布局,它允许用户根据方便安排其中的控件。它提供了“layout_alignleftof”等属性。我在答案中添加了一个参考,你去看看吧。
    • 我会看看我能用它做什么,谢谢,这有点令人困惑
    【解决方案3】:

    快速的想法:

    LinearLayout 的垂直高度设置为 0dp,并使用weight 将其他视图布局后的所有剩余空间留给它,例如给它android:layout_weight="1"

    棘手的部分是需要将高度设置为 0,因此它实际上使用了layout_weight

    警告:已经很晚了,我已经喝了几杯酒。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-25
      • 2011-08-03
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多