我有一个简单的布局,其中图像和文本居中。我希望文本始终可见,然后将其余空间用于图像。如有必要,应缩小图像。正如您在第一张图片中看到的那样,在纵向模式下一切都很好,但在横向模式下部分图像和部分文本被推出屏幕。我怎样才能解决这个问题?我事先不知道图像的大小,但我知道我的文字一直都是 54dp 的高度。此外,文本必须始终位于图像正下方(它们之间没有空格)。请参阅最后一张图片。
我想单独使用 XML 是不可能的,我将不得不在 Java 方面做一些工作。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/img"
android:src="@drawable/tree"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="54dp"
android:ellipsize="end"
android:maxLines="3"
android:text="In May 2019, Italian broadcaster RAI announced that it had begun planning the 2020 edition of the Sanremo Music Festival. It was not confirmed whether the festival would be used to select Italy's participant for 2020."
android:gravity="center"
android:textColor="#000000"
android:textSize="16dp" />
</LinearLayout>
最佳答案
这是我提出的解决方案。一旦绘制了 View 并且知道了它们的尺寸,我就会重新计算 ImageView
的大小,同时考虑图像的大小和可用区域。请参阅下图以了解其工作原理。我也稍微调整了布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/img"
android:layout_width="0dp"
android:layout_height="0dp" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="3"
android:text="In May 2019, Italian broadcaster RAI announced that it had begun planning the 2020 edition of the Sanremo Music Festival. It was not confirmed whether the festival would be used to select Italy's participant for 2020."
android:gravity="center"
android:textSize="16sp" />
</LinearLayout>
import android.graphics.BitmapFactory
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
img.setImageResource(R.drawable.tree)
img.tag = R.drawable.tree
img.viewTreeObserver.addOnGlobalLayoutListener(onGlobalLayoutListener)
}
private fun adjustImageViewSizeForImage(imgRes: Int) {
val dimensions = BitmapFactory.Options()
dimensions.inJustDecodeBounds = true
BitmapFactory.decodeResource(resources, imgRes, dimensions)
var imageViewWidth = dimensions.outWidth
var imageViewHeight = dimensions.outHeight
val availableWidth = (img.parent as ViewGroup).width
val availableHeight = (img.parent as ViewGroup).height - text.height
if (imageViewWidth > availableWidth) {
val ratio = 1.0 * availableWidth / imageViewWidth
imageViewWidth = availableWidth
imageViewHeight = (ratio * imageViewHeight).toInt()
}
if (imageViewHeight > availableHeight) {
val ratio = 1.0 * availableHeight / imageViewHeight
imageViewHeight = availableHeight
imageViewWidth = (ratio * imageViewWidth).toInt()
}
img.layoutParams = LinearLayout.LayoutParams(imageViewWidth, imageViewHeight)
}
private val onGlobalLayoutListener = object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
// view dimensions are known at this point
img.viewTreeObserver.removeOnGlobalLayoutListener(this)
adjustImageViewSizeForImage(img.tag as Int)
}
}
}
关于android - 当图像不适合屏幕时缩小图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57956275/