Android ConstraintLayout 视图位于其他视图的右侧,但位于父边界内
Android ConstraintLayout View to right of other View, but stay inside parent bounds
我在 ConstraintLayout
里面得到了两个 TextViews
。一个在左上角,另一个在第一个的右边。没什么疯狂的。现在,第一个视图可以变宽,从而将第二个视图向右推。不过,我想要的是第二个视图始终保持在父视图的范围内。
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="20dp"
android:background="#FFFFFF"
android:paddingEnd="20dp">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 1"
android:ellipsize="end"
android:maxLines="1"
android:background="#FF0000"/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@id/text1"
android:text="Text 2"
android:maxLines="1"
android:background="#00FF00"/>
...
我得到的是这样的
但是当我增加文本长度时,第二个视图被推出视图
应该发生的是第二个视图位于父视图的右上角,而第一个视图占据了可用的其余部分 space。有什么方法可以在 ConstraintLayout
中做到这一点?
你可以在activity_main.xml
中使用固定数字的android:maxWidth
,但是这样不好,因为每个phone的屏幕尺寸都不同,所以我建议你用代码设置,您可以 setMaxWidth
for text1
以下宽度 text2
和 constraintLayout
:
txt1 = (TextView) findViewById(R.id.text1);
txt2 = (TextView) findViewById(R.id.text2);
constraintLayout = (ConstraintLayout) findViewById(R.id.constraintLayout);
ViewTreeObserver vto = constraintLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
constraintLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
constraintLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
int text2Width = txt2.getWidth();
int layoutWidth = constraintLayout.getWidth();
txt1.setMaxWidth(layoutWidth-text2Width);
}
});
我能看到在 constraintLayout 中做到这一点的唯一方法是使 2 个 textView 成为一个链并将 layout_constrainedWidth
应用于第一个文本视图,以便第二个文本视图获得优先权:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:background="#fff"
tools:layout_width="200dp">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF0000"
android:ellipsize="end"
android:maxLines="1"
android:text="Text 1"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toStartOf="@id/text2"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00FF00"
android:maxLines="1"
android:text="Text 2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/text1" />
</android.support.constraint.ConstraintLayout>
短文本 1:
长文本 1:
我在 ConstraintLayout
里面得到了两个 TextViews
。一个在左上角,另一个在第一个的右边。没什么疯狂的。现在,第一个视图可以变宽,从而将第二个视图向右推。不过,我想要的是第二个视图始终保持在父视图的范围内。
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="20dp"
android:background="#FFFFFF"
android:paddingEnd="20dp">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 1"
android:ellipsize="end"
android:maxLines="1"
android:background="#FF0000"/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@id/text1"
android:text="Text 2"
android:maxLines="1"
android:background="#00FF00"/>
...
我得到的是这样的
但是当我增加文本长度时,第二个视图被推出视图
应该发生的是第二个视图位于父视图的右上角,而第一个视图占据了可用的其余部分 space。有什么方法可以在 ConstraintLayout
中做到这一点?
你可以在activity_main.xml
中使用固定数字的android:maxWidth
,但是这样不好,因为每个phone的屏幕尺寸都不同,所以我建议你用代码设置,您可以 setMaxWidth
for text1
以下宽度 text2
和 constraintLayout
:
txt1 = (TextView) findViewById(R.id.text1);
txt2 = (TextView) findViewById(R.id.text2);
constraintLayout = (ConstraintLayout) findViewById(R.id.constraintLayout);
ViewTreeObserver vto = constraintLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
constraintLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
constraintLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
int text2Width = txt2.getWidth();
int layoutWidth = constraintLayout.getWidth();
txt1.setMaxWidth(layoutWidth-text2Width);
}
});
我能看到在 constraintLayout 中做到这一点的唯一方法是使 2 个 textView 成为一个链并将 layout_constrainedWidth
应用于第一个文本视图,以便第二个文本视图获得优先权:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:background="#fff"
tools:layout_width="200dp">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF0000"
android:ellipsize="end"
android:maxLines="1"
android:text="Text 1"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toStartOf="@id/text2"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00FF00"
android:maxLines="1"
android:text="Text 2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/text1" />
</android.support.constraint.ConstraintLayout>
短文本 1:
长文本 1: