具有可拉伸视图的约束布局
Constraint layout with stretchable view
我正在尝试构建响应式布局,但我做不到。我需要实现在这些图纸上呈现的视图逻辑:
(从底部开始)有一个固定大小的视图,固定在屏幕底部。在它上面有一个TextView。它的最小高度应在中心线和底视图之间 space。当有很多文本时,它应该增长到中心线之上。在顶部有一个最大高度为 space 的 ImageView,介于屏幕顶部和中心准则之间,没有最小高度。我当前的解决方案如下所示:
<ConstraintLayout>
<ImageView
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="barrier" />
<Textview
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="bottomView" />
<View
android:layout_height="{some}dp"
app:layout_constraintBottom_toBottomOf="parent" />
<android.support.constraint.Barrier
app:barrierDirection="top"
app:constraint_referenced_ids="guidelineCenter,textView" />
<ConstraintLayout>
在这一点上,长文本的情况下工作正常,但如果文本很短,我最终会将文本锚定到底部视图。知道如何强制左图所需的最小约束吗?
据我所知,无法通过 XML 定义最小高度百分比,因此您必须在代码中强制执行最小高度。这里有两张图片显示了少量文本,另一张图片显示了较长的文本。红线在那里显示 1/2 路点在哪里,不需要。也不需要设置为 50% 的准则。
activity_main.xml
<android.support.constraint.ConstraintLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:text="This is big text "
app:layout_constraintBottom_toTopOf="@id/bottomView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<View
android:id="@+id/bottomView"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="@color/colorPrimary"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<View
android:id="@+id/view"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@android:color/holo_red_light"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/guideline" />
</android.support.constraint.ConstraintLayout>
MainActivity.xml
public class MainActivity extends AppCompatActivity {
private ConstraintLayout mLayout;
private TextView mTextView;
private View mBottomView;
private int mMinHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = findViewById(R.id.layout);
mTextView = findViewById(R.id.textView);
mBottomView = findViewById(R.id.bottomView);
mTextView.setText(getResources().getString(R.string.large_text).substring(0, 1000));
mLayout.post(new Runnable() {
@Override
public void run() {
mMinHeight = mLayout.getHeight() / 2 - mBottomView.getHeight();
if (mTextView.getHeight() < mMinHeight) {
mTextView.setMinHeight(mMinHeight);
}
}
});
}
}
我正在尝试构建响应式布局,但我做不到。我需要实现在这些图纸上呈现的视图逻辑:
<ConstraintLayout>
<ImageView
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="barrier" />
<Textview
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="bottomView" />
<View
android:layout_height="{some}dp"
app:layout_constraintBottom_toBottomOf="parent" />
<android.support.constraint.Barrier
app:barrierDirection="top"
app:constraint_referenced_ids="guidelineCenter,textView" />
<ConstraintLayout>
在这一点上,长文本的情况下工作正常,但如果文本很短,我最终会将文本锚定到底部视图。知道如何强制左图所需的最小约束吗?
据我所知,无法通过 XML 定义最小高度百分比,因此您必须在代码中强制执行最小高度。这里有两张图片显示了少量文本,另一张图片显示了较长的文本。红线在那里显示 1/2 路点在哪里,不需要。也不需要设置为 50% 的准则。
activity_main.xml
<android.support.constraint.ConstraintLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:text="This is big text "
app:layout_constraintBottom_toTopOf="@id/bottomView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<View
android:id="@+id/bottomView"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="@color/colorPrimary"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<View
android:id="@+id/view"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@android:color/holo_red_light"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/guideline" />
</android.support.constraint.ConstraintLayout>
MainActivity.xml
public class MainActivity extends AppCompatActivity {
private ConstraintLayout mLayout;
private TextView mTextView;
private View mBottomView;
private int mMinHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = findViewById(R.id.layout);
mTextView = findViewById(R.id.textView);
mBottomView = findViewById(R.id.bottomView);
mTextView.setText(getResources().getString(R.string.large_text).substring(0, 1000));
mLayout.post(new Runnable() {
@Override
public void run() {
mMinHeight = mLayout.getHeight() / 2 - mBottomView.getHeight();
if (mTextView.getHeight() < mMinHeight) {
mTextView.setMinHeight(mMinHeight);
}
}
});
}
}