0

我正在尝试创建一个在两列中排列的几个文本框的活动。我两列的宽度占屏幕的 30%,另一列占屏幕的 70%。这是我的 XML 代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_marginTop="3dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginRight="2dp"
            android:layout_weight="0.3"
            android:background="@drawable/gradient_bg_hover"
            android:gravity="center_vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="naming is fun fun is naming" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_weight="0.7"
            android:background="@drawable/gradient_bg_hover"
            android:gravity="center_vertical" >

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_marginTop="3dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginRight="2dp"
            android:layout_weight="0.3"
            android:gravity="center_vertical"
            android:background="@drawable/gradient_bg_right" >

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

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_weight="0.7"
            android:gravity="center_vertical"
            android:background="@drawable/gradient_bg_right" >

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


</LinearLayout>

</ScrollView>

问题是带有文本“hi”的列占据了屏幕的 30%,但是像“hello”这样的词占据了更多的空间并且格式被扭曲了。

无论文本的大小如何,它都应该占据屏幕的最大 30%。任何帮助表示赞赏。

4

4 回答 4

0

包含 textviews 的线性布局的 layout_width 应该是 0 dp。

利用 -

android:layout_width="0dp" 

而不是使用 -

android:layout_width="wrap_content"

因为当您定义权重时,您不必定义宽度。重量将自动处理宽度。

看看这是否有帮助。

编辑 - 这个 xml 应该可以正常工作 -

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_marginTop="3dp"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginRight="2dp"
        android:layout_weight="0.3"
        android:background="@android:color/background_dark"
        android:gravity="center_vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="naming is fun fun is naming" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="0.7"
        android:background="@android:color/background_light"
        android:gravity="center_vertical" >

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

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_marginTop="3dp"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginRight="2dp"
        android:layout_weight="0.3"
        android:gravity="center_vertical"
        android:background="@android:color/background_dark" >

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

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="0.7"
        android:gravity="center_vertical"
        android:background="@android:color/background_light" >

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

于 2013-05-29T05:19:28.627 回答
0
  1. you should consider table view for the proper formations you can specify the layout_wieght there too.

  2. set the width of the textviews to fill_parent if you are using layout_wieght because it's the allocated space for it.

  3. Use the single line enabled for the textview, if required you can marquee the text.

Edit

It's not working because of this

 <LinearLayout
            android:layout_width="wrap_content" //wrap_content
            android:layout_height="match_parent"
            android:layout_marginRight="2dp"
            android:layout_weight="0.3"
            android:gravity="center_vertical"
            android:background="@drawable/gradient_bg_right" >

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

you are not assiging the alloted space to the textview

于 2013-05-29T05:20:18.127 回答
0

Try using this for TextView. If this does'nt solves it try increasing or decreasing the value instead of 3 according to your situation

android:ems = "3"
于 2013-05-29T05:20:32.073 回答
0

If you use layout_gravity then set the corresponding height/widht to 0. Also I found not reason to use extra Nested Linear Layouts. Perhaps you can use something like this.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_marginTop="3dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_weight="0.3"
            android:text="Hi" />

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.7"
            android:text="Hi" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_marginTop="3dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_weight="0.3"
            android:text="Hi" />

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.7"
            android:text="Hi" />
    </LinearLayout>
</LinearLayout>
于 2013-05-29T05:21:01.060 回答