Sun*_*nny 8 android aspect-ratio android-recyclerview android-constraintlayout
但不知何故,图像并没有保持纵横比.
以下是用于创建回收站视图的xml文件和代码:
item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="10dp">
<ImageView
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="0dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintDimensionRatio="16:9"
tools:src="@drawable/ic_logo" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/image"
android:textColor="@color/white"
tools:text="SAMPLE"
android:paddingBottom="10dp"
android:layout_margin="5dp"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
用于生成整个视图的代码:
adapter = new SampleAdapter(list);
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
sampleRecyclerView.setLayoutManager(manager);
sampleRecyclerView.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
每个项目的宽度设置得很好,适合每个列跨度的宽度.但是高度没有保持,而且很少.
有人可以帮忙吗?
谢谢
Che*_*amp 12
对于图像,您可以指定哪个维度应与约束匹配,而另一个维度可以调整以满足比率.请参阅维度约束文档中标题为"比率"的部分.
如果两个尺寸都设置为MATCH_CONSTRAINT(0dp),您也可以使用比率.在这种情况下,系统设置最大尺寸以满足所有约束并保持指定的纵横比.根据另一方的维度约束一个特定方.您可以预先附加W,"或H"来分别约束宽度或高度.例如,如果一个维度受两个目标约束(例如,宽度为0dp并且以父项为中心),您可以通过添加来指示应该约束哪一侧字母W(用于约束宽度)或H(用于约束高度)在比率前面,用逗号分隔:
<Button android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,16:9"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
Run Code Online (Sandbox Code Playgroud)
所以,在你的情况下,我会尝试app:layout_constraintDimensionRatio="H,16:9".
另外,我不认为你需要android:scaleType="centerCrop"或者adjustViewBounds你必须尝试看看.
| 归档时间: |
|
| 查看次数: |
7255 次 |
| 最近记录: |