我有一个简单的屏幕,有4个按钮及其图像:
当我将其更改为横向模式时,图像会被裁剪并且文本丢失:
我目前使用线性布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:background="@drawable/rings" >
<LinearLayout
android:id="@+id/row1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="top|fill_vertical"
android:orientation="horizontal" >
<Button
android:id="@+id/classesButton"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@android:drawable/menuitem_background"
android:contentDescription="@string/title_activity_class_list"
android:drawableBottom="@drawable/ic_classes"
android:onClick="viewClasses"
android:scaleType="fitCenter"
android:text="@string/classes"
android:minLines="1"
android:textColor="@android:color/primary_text_light" />
<Button
android:id="@+id/studentsButton"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@android:drawable/menuitem_background"
android:drawableBottom="@drawable/ic_students"
android:onClick="viewStudents"
android:scaleType="fitCenter"
android:text="@string/students"
android:minLines="1"
android:textColor="@android:color/primary_text_light" />
</LinearLayout>
<LinearLayout
android:id="@+id/row2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="bottom|fill_vertical"
android:orientation="horizontal" >
<Button
android:id="@+id/levelsButton"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@android:drawable/menuitem_background"
android:contentDescription="@string/levels"
android:drawableBottom="@drawable/ic_levels"
android:minLines="1"
android:onClick="viewLevels"
android:scaleType="fitCenter"
android:text="@string/levels"
android:textColor="@android:color/primary_text_light" />
<Button
android:id="@+id/skillsButton"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@android:drawable/menuitem_background"
android:contentDescription="@string/skills"
android:drawableBottom="@drawable/ic_skills"
android:onClick="viewSkills"
android:scaleType="fitCenter"
android:text="@string/skills"
android:minLines="1"
android:textColor="@android:color/primary_text_light" />
</LinearLayout>
</LinearLayout>
更新我将比例类型更改为fitCenter,并期望解决问题,但事实并非如此。我真的很困惑为什么缩放不会调整图像大小以适应布局内部,因为似乎其他人发现它会缩放图像以适应屏幕。
我已尝试使用相对布局但无法找到让4个按钮同等填充屏幕的简单方法。
先谢谢,我花了太多时间在这上面!
答案 0 :(得分:1)
您可以更改图片的Scale Type
以查看最适合您的选项(由于图片的fill_parent
&amp; Weight
可能无效。
您可以制作TextViews
并将文字放在图片上方,而不是让按钮已经包含文字(个人偏好)。
如果您将我与四张图片联系起来,我可以为您解决这个问题:)
马库斯
答案 1 :(得分:0)
我终于找到了布局,这会导致缩放的图像与相应的文本一起显示。使用Buttons,ImageButtons和ImageViews进行了大量的试验和错误。我现在使用的布局包含可点击的子线性布局,其中包含文本和图像视图。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/rings"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/row1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:baselineAligned="false"
android:gravity="top|fill_vertical"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/classesLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@android:drawable/menuitem_background"
android:clickable="true"
android:onClick="viewClasses"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="@dimen/input_padding"
android:text="@string/classes"
android:textColor="@android:color/primary_text_light" />
<ImageView
android:id="@+id/classesIcon"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/ic_classes"
android:contentDescription="@string/classes"
android:minLines="1"
android:scaleType="fitCenter"
android:textColor="@android:color/primary_text_light" />
</LinearLayout>
<LinearLayout
android:id="@+id/studentsLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@android:drawable/menuitem_background"
android:clickable="true"
android:onClick="viewStudents"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="@dimen/input_padding"
android:text="@string/students"
android:textColor="@android:color/primary_text_light" />
<ImageView
android:id="@+id/studentsIcon"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/ic_students"
android:contentDescription="@string/students"
android:minLines="1"
android:scaleType="fitCenter"
android:textColor="@android:color/primary_text_light" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/row2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:baselineAligned="false"
android:gravity="bottom|fill_vertical"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/levelsLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@android:drawable/menuitem_background"
android:clickable="true"
android:onClick="viewLevels"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="@dimen/input_padding"
android:text="@string/levels"
android:textColor="@android:color/primary_text_light" />
<ImageView
android:id="@+id/levelsButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/ic_levels"
android:contentDescription="@string/levels"
android:minLines="1"
android:scaleType="fitCenter"
android:textColor="@android:color/primary_text_light" />
</LinearLayout>
<LinearLayout
android:id="@+id/skillsLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@android:drawable/menuitem_background"
android:clickable="true"
android:onClick="viewSkills"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="@dimen/input_padding"
android:text="@string/skills"
android:textColor="@android:color/primary_text_light" />
<ImageView
android:id="@+id/skillsButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:contentDescription="@string/skills"
android:src="@drawable/ic_skills"
android:minLines="1"
android:scaleType="fitCenter"
android:textColor="@android:color/primary_text_light" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
生成的肖像和风景效果图如下: