[英]How to align right a TextView in RelativeLayout which layout_width is wrap_content and it's parent layout has wrap_content to
[英]how to set textview width wrap_content but limit to 1/3 of parent's width
具有TextView
,其寬度不應超過其父級寬度的1/3。 如果其寬度小於父級的1/3,則應具有wrap_content
行為。 它的水平同級始終會在其旁邊開始。
經過嘗試,它始終具有1/3和2/3的硬剪切率,因此,如果text1的空間小於1/3,則TextView
2不會在其旁邊開始。
將LinearLayout
更改為RelativeLayout
,則android:layout_weight="n"
不起作用
基本上,需要定義width為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>
我認為您每次更新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>
碼:
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));
}
基本上,需要定義width為wrap_content,並且maxWidth不會超過1/3。
如果只需要這些,那么我的建議是取消Weight方法,並動態設置TextView的maxWidth
值。
像這樣:
tv.setMaxWidth(((LinearLayout)tv.getParent()).getWidth()/3);
解決方案很簡單:第二個Textview的寬度必須像第一個一樣為“ 0dp”。
<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="0dp"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:layout_weight="2"
/>
</LinearLayout>
如何將其放置在父級寬度的1/3的布局中?
<LinearLayout
.. this is main parent
android:weightSum="1" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
通過引入ConstraintLayout可以像這樣實現:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.333"/>
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@+id/textView1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:ellipsize="end"
android:singleLine="true"
android:text="initial text"/>
</android.support.constraint.ConstraintLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.