gpt4 book ai didi

android - 如何在线性布局上并排布局元素并应用适当的间距和对齐方式

转载 作者:行者123 更新时间:2023-12-03 20:18:34 24 4
gpt4 key购买 nike

我在元素并排在一起的线性布局的布局上遇到问题。
我可以通过将元素放在 TableView 中使它们在线性布局中并排放置,但我无法对它们应用正确的对齐方式。
例如TextView1 必须左对齐,其伙伴 TextView2 必须右对齐
即使我使用按钮也不会右对齐

android:layout_alignParentRight="true"

我正在尝试创建一个收据 Activity ,其中标题“SubTotal”、“tax”、“Total”左对齐,它们的值右对齐
下面是我的xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent"

android:paddingTop="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_main"
tools:context=".MainActivity"
android:elevation=".5dp"
android:focusable="true"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:gravity="bottom">

<TextView

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="SAN AGUSTIN MUSEUM"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textViewTicketName"/>
<TextView

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="(Comes with Audio Guide)"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textViewTicketDesc"/>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:orientation="vertical"
android:gravity="bottom">



<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/TblText1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Subtotal:"/>

<TextView
android:id="@+id/TblText11"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="$5.00"
android:layout_alignParentRight="true"/>
</TableRow>

<TextView

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"

android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Subtotal $5.00"
android:paddingLeft="0dp"
android:layout_below="@+id/textViewTicketName"

xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textSubtotal"/>




<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Tax $0.50"
android:paddingLeft="0dp"
android:layout_below="@+id/textViewTicketName"

xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textTax"/>

<View android:background="#000" android:layout_width="match_parent" android:layout_height="1px"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"

android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Total $5.50"
android:paddingLeft="0dp"
android:layout_below="@+id/textTotal"

xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textViewTotalAmount"/>

</LinearLayout>

<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" PURCHASE "
android:id="@+id/hiaButton"
android:onClick="hiaGetTicket"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"

android:textSize="18sp"
android:textColor="@color/colorWhite"
android:alpha="20"

android:background="@drawable/buttonround"/>

</LinearLayout>
</ScrollView>

这就是我的目标。在 xml 中有 2 个小计文本字段,因为我正在尝试 TablView。
它必须并排,但右侧的标题是正确的,而右侧的值也是正确的

enter image description here

到目前为止,这是正在发生的事情
使用下面的 xml


enter image description here

最佳答案

使用 layout_weight 在两个 View 之间创建一个空间。

<!-- Subtotal row -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
orientation:"horizontal">

<!--txt-->
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Subtotal"
android:gravity="right"
/>

<!-- empty space that will fill "subtotal" and "price", using layout_weight -->
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="1dp"/>

<!-- price -->
<TextView
android:layout_width="20dp"
android:layout_height="wrap_content"
android:text="Subtotal"
/>

</LinearLayout>

layout_weight 允许我们根据 subview 的“权重”来组织 subview 。看看这些例子:

1. 示例
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
orientation:"horizontal"
android:weightSum="300" --> total sum of child's weight
>

<Button
android:layout_width="0dp"
android:layout_weight="100" -> %33
android:layout_height="1dp"
android:text="Button1"
/>

<Button
android:layout_width="0dp"
android:layout_weight="100" -> %33
android:layout_height="1dp"
android:text="Button2"
/>

<Button
android:layout_width="0dp"
android:layout_weight="100" -> %33
android:layout_height="1dp"
android:text="Button3"
/>

</LinearLayout>

预习:

____Button1________Button2________Button3____(等宽,会有 %1 个空间)

2. 示例
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
orientation:"horizontal"
android:weightSum="80" --> total sum of child's weight
>

<Button
android:layout_width="0dp"
android:layout_weight="40" -> %50
android:layout_height="1dp"
android:text="Button1"
/>

<Button
android:layout_width="0dp"
android:layout_weight="20" -> %25
android:layout_height="1dp"
android:text="Button2"
/>

<Button
android:layout_width="0dp"
android:layout_weight="20" -> %25
android:layout_height="1dp"
android:text="Button3"
/>

</LinearLayout>

预习:

________Button1________Button2________Button3____(第一个较宽)

3. 例子,你的案例
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
orientation:"horizontal">

<!--txt-->
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Subtotal"
android:gravity="right"
/>

<View
android:layout_width="0dp"
android:layout_weight="777" --> this will fill empty space, we didn't specify weightSum, so it thinks weightSum is 777.
android:layout_height="5dp"
android:background="#ff0000"
/>

</LinearLayout>

关于android - 如何在线性布局上并排布局元素并应用适当的间距和对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37833352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com