Java Android:2个文本视图包含长文本的布局问题
我无法让两个文本视图按我想要的方式运行 我想要一个在左边,另一个在右边。我相当简单地通过水平线性布局实现了这一点。如果字符串太长,它们都应该只使用一半的水平空间保持在它们的一侧,并垂直扩展 但是,如果其中一个或两个文本视图都包含很长的字符串,则会根据设置权重的方式得到奇怪的结果 例如,如果我将权重设置为Left=1,Right=1,那么如果两条线都短或都长,一切都正常。但是,如果它们不同,则会缩小短字符,使其仅显示在每行字符上。使用0/0权重,较短的TextView完全消失 如果我将短字符串文本视图设置为0,将长字符串文本视图设置为1或更高,我可以让它工作,但这只包括一种情况 我还尝试了TableLayout和RelativeLayout,但没有任何效果 亲切问候,, 水母 编辑: 通过将文本视图放入水平布局内的垂直线性布局中解决了此问题:Java Android:2个文本视图包含长文本的布局问题,java,android,layout,Java,Android,Layout,我无法让两个文本视图按我想要的方式运行 我想要一个在左边,另一个在右边。我相当简单地通过水平线性布局实现了这一点。如果字符串太长,它们都应该只使用一半的水平空间保持在它们的一侧,并垂直扩展 但是,如果其中一个或两个文本视图都包含很长的字符串,则会根据设置权重的方式得到奇怪的结果 例如,如果我将权重设置为Left=1,Right=1,那么如果两条线都短或都长,一切都正常。但是,如果它们不同,则会缩小短字符,使其仅显示在每行字符上。使用0/0权重,较短的TextView完全消失 如果我将短字符串文本
<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>
对我来说似乎有点太大了。。。但显然没有其他解决方案了?如果我将一个字母字符串设置为第一个文本视图,将很长的字符串设置为第二个文本视图,那么这对我来说很好
<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>
为了让android:layout\u权重正常工作,需要将要加权的维度设置为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只会产生相同的结果。如果一个字符串长而另一个短,那么短的字符串会被挤压到一边。您是否将两个字符串的布局宽度都设置为0?这在我的测试中起作用。哦,我明白了。布局宽度为0时,它确实可以工作。不过,我确实更喜欢这种变化来匹配父母。更清楚一点。。。如果只阅读代码,宽度0听起来不太符合逻辑。^无论如何谢谢你!我很高兴你有了解决办法代码可读性确实应该有一个使用权重设置。哇,谢谢!填充父项确实起到了作用,因此它与布局权重完全无关。。。很酷-