LinearLayout小部件对齐

时间:2015-06-29 13:22:29

标签: android

完整的xml就是这样,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.brightspot.extrain5psim.view.fragments.NetworthFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:baselineAligned="false"
        android:orientation="horizontal"
        android:weightSum="2">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#fafafa"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Some text goes here"
                android:textSize="22sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:text="Some text goes here Some text goes here" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#cafafa"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Some text goes here Some text goes here Some text goes here Some text goes here" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="#fafafa"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Some text goes here" />
    </LinearLayout>
</LinearLayout>

这将提供以下输出。

enter image description here

如果我将android:textSize="22sp"添加到第二个TextView,我也可以获得以下屏幕截图。

enter image description here

请检查这些文本之间的区别。

我的问题是为什么TextView对齐取决于之前TextView的文字大小,以及如何解决此问题?

2 个答案:

答案 0 :(得分:0)

我想,你想要摆脱重量总和。您可能不需要它,重量总和通常会自动计算。

如果这是以编程方式生成的,那么您将要使用权重。我在测试中发现,消除重量总和然后在两个文本视图中增加重量似乎已经解决了你所面临的问题。如果没有整体布局,我无法完全看到这是否能解决您的问题,但是当我使用这个布局配置进行测试和播放时(至少尽我所能,显然我不会&#39) ;你有你的风格/颜色)。

祝你好运!我希望它有所帮助。

答案 1 :(得分:0)

我想通了,问题是由于layout_width相互之间设置不正确而引起的,这段代码工作正常

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.brightspot.extrain5psim.view.fragments.NetworthFragment">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:baselineAligned="false"
    android:orientation="horizontal"
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#fafafa"
        android:orientation="horizontal"
        android:weightSum="2"
        >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Some text goes here "
            android:textSize="22sp"
            android:textStyle="bold"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="4dp"
            android:paddingBottom="10dp"
            android:text="text goes here Some text goes here"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#cafafa"
        android:orientation="horizontal"
        android:weightSum="1">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:text="Some text goes here Some text goes here Some text goes here Some text goes here ,Some text goes here Some text goes here Some text goes here Some text goes here"
            />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:background="#fafafa"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some text goes here" />
</LinearLayout>

我已经使用layout_weightSum正确设置textview,你可以拥有任意数量的行

更新更短的实施:更具可读性

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.brightspot.extrain5psim.view.fragments.NetworthFragment">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:baselineAligned="false"
    android:orientation="horizontal"
    >


        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Some text goes here"
            android:textSize="22sp"
            android:textStyle="bold"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Some text also goes here and it should be here "
            android:layout_weight="1"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="4dp"
            android:text="Some text goes here Some text goes here"
            android:layout_weight="1"/>


</LinearLayout>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Some text goes here also" />

</LinearLayout>