是否可以缩放ImageView,使其下面的LinearLayout始终完全可见?
问题描述:
我的布局如下。在一些mdpi屏幕上,内部的LinearLayout被截断。我想要发生的是确保内部的LinearLayout始终显示它的所有内容。我希望ImageView能够尽可能小,以确保它下面的LinearLayout显示它的所有内容。在大屏幕上,ImageView应该与其图像一样大(即wrap_content
)。是否可以缩放ImageView,使其下面的LinearLayout始终完全可见?
我试图给ImageView的0.1的layout_weight
和layout_weight
0.9(并给予两者0dp的layout_height
的),但是然后在更大屏幕上的ImageView的结束是在布局的顶部的LinearLayout。我希望根LinearLayout的内容居中。
所以,只有在需要的时候才可以缩小ImageView,使得它下面的LinearLayout始终完全可见?我正试图完成这项功能,为不同的屏幕尺寸/分辨率创建多个布局文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/tab_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".messages.MessagesHomeFragment"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:src="@drawable/elephant_large" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
>
<Button
android:id="@+id/button_send_voice_message"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Send a Voice Message" />
<Button
android:id="@+id/button_send_sms_message"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Send an SMS" />
<Button
android:id="@+id/button_view_messages"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Manage Messages" />
<Button
android:id="@+id/button_invite_sms_recipients"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Invite SMS Recipients" />
</LinearLayout>
</LinearLayout>
答
我能够完成我试图通过使用下面的布局做。关键是将根LinearLayout的高度设置为wrap_content
并将ImageView的layout_weight
设置为1.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/tab_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
>
<ImageView
android:id="@+id/elephantImageView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:layout_marginBottom="20dp"
android:src="@drawable/elephant_large"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
>
<Button
android:id="@+id/button_send_voice_message"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Send a Voice Message" />
...
</LinearLayout>
</LinearLayout>
答
我不知道这是可能的......如果有的话,我会尝试像下面描述的布局(我没有尝试,恐怕图像将成长为多,因为它可以用的东西像那样)。我只更改了ImageView的布局属性。如果仍然不起作用,那么定义基于ImageView的自己的小部件应该不会太困难,因为您只需要覆盖onLayout方法以仅缩小measureSpec你得到的是小于图像...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/tab_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".messages.MessagesHomeFragment"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerInside"
android:layout_marginBottom="40dp"
android:src="@drawable/elephant_large" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
>
<Button
android:id="@+id/button_send_voice_message"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Send a Voice Message" />
<Button
android:id="@+id/button_send_sms_message"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Send an SMS" />
<Button
android:id="@+id/button_view_messages"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Manage Messages" />
<Button
android:id="@+id/button_invite_sms_recipients"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Invite SMS Recipients" />
</LinearLayout>
</LinearLayout>