【问题标题】:Combine two LinearLayouts into one LinearLayout将两个 LinearLayout 合并为一个 LinearLayout
【发布时间】:2016-05-20 19:42:01
【问题描述】:

我在RelativeLayout 中有两个LinearLayout。我只想在一个 LinearLayout 内执行此 LinearLayout,以便我的子控件将显示在 1 LinearLayout 内。这样我就可以将这个 LinearLayout 分组并显示 使用不同的背景颜色android:background="@drawable/my_custom_background,以便所有子控件都耦合到其中。

请看下面的屏幕截图 代码

<?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"
    android:orientation="vertical"
    android:background="#0B95BA"
    android:minWidth="25px"
    android:minHeight="25px">
    <LinearLayout
        android:id="@+id/linearLayoutCont"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="5dp">
        <TextView
            android:id="@+id/txtViewCont"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Contact Billing"
            android:gravity="center"
            android:textSize="25sp"
            android:textColor="#FFFFFF" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearLayoutContBillingCall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@id/linearLayoutCont"
        android:layout_marginTop="5dp">
        <Button
            android:text="Call"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:textColor="#FFFFFF"
            android:layout_gravity="right"
            android:background="@drawable/ButtonStyle"
            android:id="@+id/btnContCall"
            android:drawableLeft="@drawable/PhoneCall" />
        <Button
            android:text="Email"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:textColor="#FFFFFF"
            android:layout_gravity="right"
            android:background="@drawable/ButtonStyle"
            android:id="@+id/btnEmail"
            android:drawableLeft="@drawable/Email" />
    </LinearLayout>
</RelativeLayout>

和 o/p 应该在图片上方,例如 Contact 在中心,两个按钮(Call 和 Email)在联系人同一个角落下方。还有一件事我想问我如何在这两个按钮之间创建一个空间。

【问题讨论】:

  • 您正在尝试将 2 个具有不同方向的 LinearLayouts 组合在一起 - 无法将它们组合成一个。只需使用 RelativeLayout 来定位控件,而无需任何 LinearLayouts。
  • @adelphus 我试过了,但没有显示子控件。
  • @adelphus 我试过 android:id="@+id/linearLayoutCont" 它在一行中显示我的子控件 Contact 、 Call 、 Email ,即单行我希望它在上面的 COntact 和 Call and下面的电子邮件按钮。
  • 这就是为什么你使用 layout_below、layout_toLeftOf 和其他类似的属性来定位 RelativeLayouts 中的元素。我认为您需要阅读有关如何使用 RelativeLayout 的文档。
  • 您可以使用线性布局而不是相对布局,然后在其中嵌套具有所需方向、宽度和高度的其他线性布局。

标签: android android-layout xamarin xamarin.android


【解决方案1】:

您只需要使用一个Linear Layout 而不是两个。您的 Text View 是您的 Relative Layout 的子代。我已对您的Layout 进行了一些修改。

参考这个。

<?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"
    android:background="#0B95BA">


    <TextView
        android:id="@+id/txtViewCont"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Contact Billing"
        android:textColor="#FFFFFF"
        android:textSize="25sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtViewCont"
        android:orientation="horizontal"
        android:weightSum="2">


        <Button
            android:id="@+id/btnContCall"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:drawableLeft="@mipmap/ic_launcher"
            android:drawablePadding="5dp"
            android:text="Call"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btnEmail"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:drawableLeft="@mipmap/ic_launcher"
            android:drawablePadding="5dp"
            android:text="Email"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

    </LinearLayout>
</RelativeLayout>

注意:应用你自己的 Drawables 和 Background,就像你在你的 OP 中一样。

【讨论】:

  • 感谢回复我有 1 个问题 android:drawableLeft="@mipmap/ic_launcher" ????
  • 这是一个Drawable 图像,正如您在代码android:drawableLeft="@drawable/PhoneCall" 中使用的那样,与此相同。按照我的回答顺序,让它成为你所拥有的。
  • 如果对您有帮助,请接受并投票支持:)。
  • 我还有一个问题,如果我在里面还有一个线性布局,我该怎么做。表示相同的 TextView 和 2 个按钮
  • 如果你使用这种类型的 UI 性能将不会像你预期的那样。您将无法再使用Relative Layout's 属性。最好的办法就是这样。如果您需要更多两个按钮,则可以使用新的 1 Linear Layout 并将其添加到其他布局下方。
【解决方案2】:

将 relative 替换为 linear 你会得到你想要的输出。

<?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"
    android:background="#0B95BA"
    android:minWidth="25px"
    android:minHeight="25px">
    <LinearLayout
        android:id="@+id/linearLayoutCont"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="5dp">
        <TextView
            android:id="@+id/txtViewCont"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Contact Billing"
            android:gravity="center"
            android:textSize="25sp"
            android:textColor="#FFFFFF" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearLayoutContBillingCall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@id/linearLayoutCont"
        android:layout_marginTop="5dp">
        <Button
            android:text="Call"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:textColor="#FFFFFF"
            android:layout_gravity="right"
            android:background="@drawable/ButtonStyle"
            android:id="@+id/btnContCall"
            android:drawableLeft="@drawable/PhoneCall" />
        <Button
            android:text="Email"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:textColor="#FFFFFF"
            android:layout_gravity="right"
            android:background="@drawable/ButtonStyle"
            android:id="@+id/btnEmail"
            android:drawableLeft="@drawable/Email" />
    </LinearLayout>
</LinearLayout>

【讨论】:

  • 我想要 TextView 和按钮的 LinearLayout。
  • 这就是代码中的内容。 LineayLayout 用于 Textview 和 LineraLayout 用于按钮
  • 我已经实现了,你只需用 LinearLayout 替换相对布局。我想要一个布局中的 TextView 和 Button
  • 我认为使用线性布局是不可能的。因为您希望按钮并排,因此您需要另一种布局。如果您想为两者使用一种布局,请选择网格布局。 developer.android.com/reference/android/widget/GridLayout.html
  • 无论情况如何,您都不应该这样做。嵌套的 LinearLayout 性能很差。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
  • 2012-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多