lan*_*nyf 10 android textview android-layout-weight
有一个TextView,它的宽度不应超过其父宽度的1/3.如果它的宽度小于父级的1/3,它应该有wrap_content行为.它的横向兄弟将始终在它旁边开始.
尝试下面,它总是有1/3和2/3的硬切,所以如果text1的空间小于1/3,则TextView两个不会在它旁边开始.
改变LinearLayout为RelativeLayout,然后android:layout_weight="n"不起作用
基本上,需要定义宽度是wrap_content和maxWidth不超过1/3.
有什么建议吗?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="end"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:singleLine="true"
android:ellipsize="end"
android:layout_weight="2"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我认为每次更新 textView 时,您都必须动态检查父布局宽度,例如(我已经使用按钮测试了此代码并编辑文本以更改 textView - 工作没有问题):
<LinearLayout
android:id="@+id/myLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tvA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
/>
<TextView
android:id="@+id/tvB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
代码:
TextView tvA = (TextView) findViewById(R.id.tvA);
TextView tvB = (TextView) findViewById(R.id.tvB);
LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);
// code to use when the textView is updated
// possibly button onClickListener?
tvA.measure(0, 0);
int textWidth = tvA.getMeasuredWidth();
myLayout.measure(0,0);
int layoutWidth = myLayout.getWidth();
if (textWidth > (layoutWidth / 3)) {
tvA.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
tvB.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2.0f));
} else {
tvA.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
tvB.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8842 次 |
| 最近记录: |