Android布局: left item in horizontal LinearLayout forces right justified text to jumble

标签 android android-layout

wowrly?

这就是我所拥有的。我需要组数据中的那些粗体标题来换行,而不是右侧的数据。这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
    android:paddingRight="5dip"
    android:paddingBottom="5dip"
    android:paddingTop="5dip">
    <TextView
        android:id="@+id/igi_product"
        android:textStyle="bold"
        android:textSize="18sp"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0">
        <TableRow>
            <TextView
                android:id="@+id/igi_units"
                android:gravity="right"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"/>
            <TextView
                android:id="@+id/igi_uom"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"/>
        </TableRow>
        <TableRow>
            <TextView
                android:id="@+id/igi_total"
                android:gravity="right"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"/>
        </TableRow>
        <TableRow>
            <TextView
                android:id="@+id/igi_price"
                android:gravity="right"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"/>
        </TableRow>
    </TableLayout>
</LinearLayout>

编辑:

我尝试用RelativeLayout替换LinearLayout,结果如下:

wowrly?

代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
    android:paddingRight="5dip"
    android:paddingBottom="5dip"
    android:paddingTop="5dip">
    <TextView
        android:id="@+id/igi_product"
        android:textStyle="bold"
        android:textSize="18sp"
        android:layout_alignParentLeft="true"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true">
        <TableRow>
            <TextView
                android:id="@+id/igi_units"
                android:gravity="right"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"/>
            <TextView
                android:id="@+id/igi_uom"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"/>
        </TableRow>
        <TableRow>
            <TextView
                android:id="@+id/igi_total"
                android:gravity="right"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"/>
        </TableRow>
        <TableRow>
            <TextView
                android:id="@+id/igi_price"
                android:gravity="right"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"/>
        </TableRow>
    </TableLayout>
</RelativeLayout>

最佳答案

出于这样的目的,我强烈推荐RelativeLayout
由于我在 LinearLayout 方面也遇到了很多(最微小的)问题,
RelativeLayout 对我来说非常有用。

好资源:http://www.mkyong.com/android/android-relativelayout-example/

否则: 尝试在困惑的 TextView 中使用 android:layout_width="fill_parent" ,或者给它一个权重属性 (android:layout_weight="1") 或尝试使用 android:minWidth="50dp" ...

也许其中之一适合您。

关于Android布局: left item in horizontal LinearLayout forces right justified text to jumble,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10934550/

相关文章:

Android 横向和纵​​向模式变化

android - "No XML content. Please add a root view or layout to your document."

android - 从 Android 中的 Google 日历获取颜色代码

android - 显示标记而不在屏幕上跳转

java - 通过代码以 dp 为单位设置 View 的高度?

android - GridView 可以有跨越一行或多行的列吗

java - Android 中的共享元素转换

android - 使用按钮在 Android 中打开另一个 Activity

android - 以编程方式将按钮添加到布局,但没有出现

android - 塑造 Android Material Design