【问题标题】:Android: Layout problem with 2 textviews containing long textAndroid:包含长文本的 2 个文本视图的布局问题
【发布时间】:2011-04-26 23:35:12
【问题描述】:

我无法让两个 Textview 以我想要的方式运行。

我想要一个在左边,另一个在右边。我相当简单地通过水平线性布局实现了这一点。如果字符串太长,他们都应该只使用一半的水平空间(“留在他们身边”)并垂直扩展。

但是,如果一个或两个文本视图包含很长的字符串,我会得到奇怪的结果,具体取决于我设置权重的方式。

例如如果我将权重设置为 Left=1,Right=1 如果两个字符串都很短或两个字符串都很长,则一切正常。但是,如果它们不同,则短的将缩小为仅在每行字符上显示。使用 0/0 权重,较短的 TextView 就完全消失了。

如果我将短字符串文本视图设置为“0”,将长字符串文本视图设置为“1”或更高,我可以让它工作,但这仅涵盖一种情况。

我也尝试过 TableLayout 和 RelativeLayout,但没有任何效果。

亲切的问候, 水母

编辑: 通过将 TextViews 放入水平布局内的垂直 LinearLayouts 来解决它:

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

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

        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:gravity="right"
            android:layout_gravity="right">
        </TextView>

    </LinearLayout>

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

        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:gravity="right"
            android:layout_gravity="right">
        </TextView>

    </LinearLayout>

</LinearLayout>

虽然对我来说似乎有点过大......但显然没有其他解决方案?

【问题讨论】:

    标签: java android layout


    【解决方案1】:

    如果我将一个字母字符串设置为第一个 textView 并将非常长的字符串设置为第二个 textView,这对我来说很好

    <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" 
        xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal">
        <TextView android:id="@+id/textView1" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:layout_weight="1"></TextView>
        <TextView android:id="@+id/textView2" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:layout_weight="1"></TextView>
    </LinearLayout>
    

    【讨论】:

    • 哇,谢谢! “fill_parent”确实有所作为,所以它与布局重量完全无关......非常酷。 :-)
    【解决方案2】:

    为了使 android:layout_weight 工作,您需要将您想要加权的维度设置为 0。此外,虽然这不是必需的,但它有助于让您的总权重加起来一个您可以轻松使用的值破译成百分比。我喜欢用分数,总和是 1。其他人喜欢用 100 作为总和。因此,对于两个并排编辑文本,每个文本占用一半布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        android:orientation="horizontal">
    
            <TextView  
                android:layout_width="0dip" 
                android:layout_height="wrap_content"
                android:layout_weight=".5"
                android:gravity="right"
                android:layout_gravity="right">
            </TextView>
    
            <TextView  
                android:layout_width="0dip" 
                android:layout_height="wrap_content"
                android:layout_weight=".5"
                android:gravity="right"
                android:layout_gravity="right">
            </TextView>
    </LinearLayout>
    

    【讨论】:

    • 感谢您的提示,但 0.5/0.5 只会产生相同的结果。如果一根弦长而另一根短,短的那根被挤到一边。
    • 您是否将两者的 layout_width 都设置为 0?这在我的测试中有效。
    • 哦,我明白了。使用 layout_width "0" 确实可以正常工作。不过,我确实更喜欢更改为“匹配父级”。让它更清楚一点......如果你只阅读代码,宽度 0 听起来不太合乎逻辑。 ^^ 还是谢谢!
    • 很高兴您有解决方案。 :-) 确实应该有一个“use_weight”设置以提高代码的可读性。
    猜你喜欢
    • 2014-08-09
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多