【问题标题】:XML Android layout problemsXML Android 布局问题
【发布时间】:2014-01-19 18:10:20
【问题描述】:

所以我遇到了一些 XML 布局编码的问题......我应该复制这张图片

但到目前为止,我似乎只能做到这一点,即使我确实使用 android:gravity="right" 在其容器内浮动... :/

这是我目前的代码:

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".CartActivity" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
   >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:text="Shopping Cart"
        android:background="@android:color/holo_blue_dark" />

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@android:color/holo_blue_light"
         >

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#800000"
            android:text="£???" />

        <Button
            android:id="@+id/checkout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="Checkout" >
        </Button>
    </LinearLayout>
</LinearLayout>

【问题讨论】:

  • 问题解决了吗?
  • 非常感谢,问题解决了 :) 但是我不需要 android:weightSum="3",使用它实际上使它无处不在。但是将 android:layout_weight="1" 放入两个文本视图中解决了问题,因此结帐按钮不需要重新对齐:)

标签: android xml layout gravity layout-gravity


【解决方案1】:

为你的内心使用android:weightSum="3"

三个元素的布局,然后,

android:layout_weight="1" 到每个视图

这将使视图占据可用空间的 1/3

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@android:color/holo_blue_light"
    android:weightSum="3"
     >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Subtotal:" 
        android:layout_weight="1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#800000"
        android:text="£???"
        android:layout_weight="1" />

    <Button
        android:id="@+id/checkout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="Checkout" 
        android:layout_weight="1">
    </Button>
</LinearLayout>

EDIT fill_parent 已弃用,使用 match_parent

【讨论】:

  • 如果你的 android:layout_width="0dp" 最好。此外,您可以使 android:weightSume="100"(3 也很好) 并使孩子不相等,例如。 20(0,5), 20(0,5), 60(2)。
【解决方案2】:

尝试使用相对布局

<RelativeLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Subtotal:" />

    <TextView
        android:id="@+id/textView2"
        android:layout_toRightOf="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#800000"
        android:text="£???" />

    <Button
        android:id="@+id/checkout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Checkout" >
    </Button>
</RelativeLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多