两个TextView,我想水平各占屏幕的一半。如何改写布局?

中才德创 2014-05-31 11:30:18
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/row_tax"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />

<TextView
android:id="@+id/row_income"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />

</LinearLayout>
...全文
1305 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
业余的CODER 2014-06-09
  • 打赏
  • 举报
回复
weight 设置成1:1 width 设置成0dp 注:设置成0dp可以提高效率,至少谷歌是这么说的。 width如果分别设置成不同的值,同时weight设置1,它们的宽度会按照比例进行分配。 好像是TextView 的宽度比等于 各自(weight×width)的比值。 没测试过,看到问题回忆好像是这个样子!!希望对你有用!
中才德创 2014-06-08
  • 打赏
  • 举报
回复
引用 10 楼 u011879633 的回复:
设置权值就可以了~也就是weight
实测不行。1 : 1不是真的1 : 1.
破碎小小多 2014-06-07
  • 打赏
  • 举报
回复
设置权值就可以了~也就是weight
中才德创 2014-06-07
  • 打赏
  • 举报
回复
为什么,第一二行的控件,没有平分水平宽度呢?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent">
    <TableLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent">
		<TableRow>
        	<TextView		        
		        android:layout_height="wrap_content"
		        android:layout_weight="0.5"
		        android:text="@string/income" >
			</TextView>
		    <EditText
				android:layout_height="wrap_content"				
				android:layout_weight="0.5"
				android:id="@+id/income"
				android:numeric="integer" >
		    </EditText>
	    </TableRow>
	    <TableRow>
		    <TextView
		        android:layout_height="wrap_content"
		        android:layout_weight="0.5"
		        android:text="@string/insurance" >
			</TextView>
	        <EditText
				android:layout_height="wrap_content"
				android:layout_weight="0.5"
				android:id="@+id/insurance"
				android:numeric="integer" >          
	        </EditText>
        </TableRow>
	</TableLayout>    
    <Button 
        android:layout_height="wrap_content" 
        android:text="@string/pit_btn" 
        android:layout_width="fill_parent" 
        android:id="@+id/submit">        
    </Button>
    <TextView 
        android:id="@+id/result" 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent">        
    </TextView>
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/purincome">        
    </TextView>
</LinearLayout>
未注销 2014-06-07
  • 打赏
  • 举报
回复
引用 4 楼 HawkOfWinter 的回复:
实测了下,还得作些改动,方可: 1)android:layout_width="fill_parent",不能用wrap_content。 2)android:layout_width="wrap_content",不必改成0dp。这0dp,相当于宽度写成0像素,没必要,反而容易给人误解。
<?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:orientation="horizontal" >
 
    <TextView
        android:id="@+id/row_tax"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />
     
    <TextView
        android:id="@+id/row_income"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />
 
</LinearLayout>
To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1".
zhbd1989 2014-06-03
  • 打赏
  • 举报
回复
引用 4 楼 HawkOfWinter 的回复:
实测了下,还得作些改动,方可: 1)android:layout_width="fill_parent",不能用wrap_content。 2)android:layout_width="wrap_content",不必改成0dp。这0dp,相当于宽度写成0像素,没必要,反而容易给人误解。
<?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:orientation="horizontal" >
 
    <TextView
        android:id="@+id/row_tax"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />
     
    <TextView
        android:id="@+id/row_income"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />
 
</LinearLayout>
1:1什么都行吧。。。
Linux-Torvalds 2014-06-02
  • 打赏
  • 举报
回复
relativelayout alignparentLeft alignparentRight
韩曙亮 2014-06-02
  • 打赏
  • 举报
回复
<?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:orientation="horizontal" >
  
    <TextView
        android:id="@+id/row_tax"
        android:layout_width="match_content"
        android:layout_height="match_content"
        android:layout_weight="1"
        android:text="TextView" />
      
    <TextView
        android:id="@+id/row_income"
        android:layout_width="match_content"
        android:layout_height="match_content"
        android:layout_weight="1"
        android:text="TextView" />
  
</LinearLayout>
1:1 怎么玩都行
中才德创 2014-06-01
  • 打赏
  • 举报
回复
实测了下,还得作些改动,方可: 1)android:layout_width="fill_parent",不能用wrap_content。 2)android:layout_width="wrap_content",不必改成0dp。这0dp,相当于宽度写成0像素,没必要,反而容易给人误解。
<?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:orientation="horizontal" >
 
    <TextView
        android:id="@+id/row_tax"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />
     
    <TextView
        android:id="@+id/row_income"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />
 
</LinearLayout>
riguangc 2014-06-01
  • 打赏
  • 举报
回复
楼上正解!
fire_android 2014-06-01
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
 
    <TextView
        android:id="@+id/row_tax"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />
     
    <TextView
        android:id="@+id/row_income"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />
 
</LinearLayout>
tcmakebest 2014-06-01
  • 打赏
  • 举报
回复
让两个TextView 都设成 width=0dp, weight=1 两个 weight 就是大小比值,设成相同就是一样大。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧