设置水平链接的图像,对高度和宽度进行匹配约束,并且具有比率,这在XML中效果很好,但不能以编程方式实现
我在xml中工作的布局。图像从左右延伸,其大小取决于可用的屏幕空间。
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="TEST"
android:id="@+id/nameText"/>
<ImageView
android:layout_width="0dp"
android:layout_height="0dp" tools:srcCompat="@tools:sample/avatars"
android:id="@+id/first"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintEnd_toStartOf="@+id/second"
app:layout_constraintTop_toBottomOf="@+id/nameText" app:layout_constraintDimensionRatio="w,1:1"/>
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/firstName"
tools:text="NAMETEST"
app:layout_constraintTop_toBottomOf="@+id/first"
app:layout_constraintLeft_toLeftOf="@+id/first"
app:layout_constraintRight_toRightOf="@+id/first"/>
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/firstVotes"
tools:text="NAMETEST"
app:layout_constraintTop_toBottomOf="@+id/firstName"
app:layout_constraintLeft_toLeftOf="@+id/first"
app:layout_constraintRight_toRightOf="@+id/first"/>
<ImageView
android:layout_width="0dp"
android:layout_height="0dp" tools:srcCompat="@tools:sample/avatars"
android:id="@+id/second"
app:layout_constraintStart_toEndOf="@+id/first"
app:layout_constraintTop_toTopOf="@+id/first" app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintEnd_toStartOf="@+id/third" app:layout_constraintDimensionRatio="w,1:1"
android:layout_marginStart="20dp" android:layout_marginEnd="20dp"/>
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/secondName"
tools:text="NAMETEST"
app:layout_constraintTop_toBottomOf="@+id/second"
app:layout_constraintLeft_toLeftOf="@+id/second"
app:layout_constraintRight_toRightOf="@+id/second"/>
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/secondVotes"
tools:text="NAMETEST"
app:layout_constraintTop_toBottomOf="@+id/secondName"
app:layout_constraintLeft_toLeftOf="@+id/second"
app:layout_constraintRight_toRightOf="@+id/second"/>
<ImageView
android:layout_width="0dp"
android:layout_height="0dp" tools:srcCompat="@tools:sample/avatars"
android:id="@+id/third"
app:layout_constraintStart_toEndOf="@+id/second"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/second" app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintDimensionRatio="1:1"/>
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/thirdName"
tools:text="NAMETEST"
app:layout_constraintTop_toBottomOf="@+id/third"
app:layout_constraintLeft_toLeftOf="@+id/third"
app:layout_constraintRight_toRightOf="@+id/third"/>
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/thirdVotes"
tools:text="NAMETEST"
app:layout_constraintTop_toBottomOf="@+id/thirdName"
app:layout_constraintLeft_toLeftOf="@+id/third"
app:layout_constraintRight_toRightOf="@+id/third"/>
</android.support.constraint.ConstraintLayout>
(我相信是)上面的确切镜像,但是用代码完成!图片可以正确地链接和隔开,我可以通过TextViews进行验证,但是图片会丢失所有高度和宽度,并且根本不会显示在我的应用中。
private class LeaderBoardView(ct: Context, leaderBoard: LeaderBoard): ConstraintLayout(ct) {
init {
this.id = View.generateViewId()
val cs = ConstraintSet()
cs.clone(this)
val roleName = TextView(ct)
roleName.text = leaderBoard.roleName
roleName.id = View.generateViewId()
roleName.setTextColor(Color.WHITE)
roleName.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 17F)
cs.connect(roleName.id, TOP, PARENT, TOP)
cs.connect(roleName.id, START, PARENT, START)
cs.constrainWidth(roleName.id, WRAP)
cs.constrainHeight(roleName.id, WRAP)
this.addView(roleName)
val first = ImageView(ct)
first.id = View.generateViewId()
cs.connect(first.id, TOP, roleName.id, BOTTOM)
cs.constrainHeight(first.id, MATCH)
cs.constrainWidth(first.id, MATCH)
cs.setDimensionRatio(first.id,"1:1")
this.addView(first)
Picasso.get().load(leaderBoard.first!!.headshotURL).transform(PicassoCircleTransform()).into(first)
val firstName = TextView(ct)
firstName.id = View.generateViewId()
firstName.text = leaderBoard.first!!.firstName
firstName.setTextColor(Color.WHITE)
cs.connect(firstName.id, START, first.id, START)
cs.connect(firstName.id, END, first.id, END)
cs.connect(firstName.id, TOP, first.id, BOTTOM)
cs.constrainWidth(firstName.id, WRAP)
cs.constrainHeight(firstName.id, WRAP)
this.addView(firstName)
val firstVotes = TextView(ct)
firstVotes.id = View.generateViewId()
firstVotes.text = leaderBoard.first!!.firstName
firstVotes.setTextColor(Color.WHITE)
cs.connect(firstVotes.id, START, first.id, START)
cs.connect(firstVotes.id, END, first.id, END)
cs.connect(firstVotes.id, TOP, firstName.id, BOTTOM)
cs.constrainWidth(firstVotes.id, WRAP)
cs.constrainHeight(firstVotes.id, WRAP)
this.addView(firstVotes)
val second = ImageView(ct)
second.id = View.generateViewId()
cs.connect(second.id, TOP, first.id, TOP)
cs.constrainHeight(second.id, MATCH)
cs.constrainWidth(second.id, MATCH)
cs.setDimensionRatio(second.id,"1:1")
this.addView(second)
Picasso.get().load(leaderBoard.second!!.headshotURL).transform(PicassoCircleTransform()).into(second)
val secondName = TextView(ct)
secondName.id = View.generateViewId()
secondName.text = leaderBoard.first!!.firstName
secondName.setTextColor(Color.WHITE)
cs.connect(secondName.id, START, second.id, START)
cs.connect(secondName.id, END, second.id, END)
cs.connect(secondName.id, TOP, second.id, BOTTOM)
cs.constrainWidth(secondName.id, WRAP)
cs.constrainHeight(secondName.id, WRAP)
this.addView(secondName)
val secondVotes = TextView(ct)
secondVotes.id = View.generateViewId()
secondVotes.text = leaderBoard.first!!.firstName
secondVotes.setTextColor(Color.WHITE)
cs.connect(secondVotes.id, START, second.id, START)
cs.connect(secondVotes.id, END, second.id, END)
cs.connect(secondVotes.id, TOP, secondName.id, BOTTOM)
cs.constrainWidth(secondVotes.id, WRAP)
cs.constrainHeight(secondVotes.id, WRAP)
this.addView(secondVotes)
cs.createHorizontalChainRtl(PARENT, START, PARENT, END, intArrayOf(first.id, second.id), null, ConstraintSet.CHAIN_SPREAD)
cs.applyTo(this)
}```
答案 0 :(得分:0)
尝试一下
//您还可以设置imageView的ScalType属性
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="fitXY"
android:id="@+id/img"/>
// also set placeholder in picasso
Picasso.with(context).load(img url).placeholder(default_img).into(view name);
// you can also resize image with picasso
Picasso.with(context).load(img url).placeholder(default_img).reSize(width,height).into(view name);