【发布时间】:2016-05-11 10:06:24
【问题描述】:
我有几个线性布局(见底部的图片和一段代码)。每个按钮上都有一个文本,文本下有一个图像。但是有一个问题。当应用程序在分辨率较低的手机上运行时,图像会隐藏文本。如何防止这种情况发生?
我将 layout_height 设置为 0pt 以让 layout_weight 处理这个问题。
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0pt"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_weight="1"
android:layout_width="0pt"
android:layout_height="match_parent"
android:text="@string/button_pin1"
android:background="@drawable/button_blue"
android:layout_margin="2dp"
android:drawableBottom="@mipmap/ic_arrow_left" />
<Button
android:id="@+id/button2"
android:layout_weight="1"
android:layout_width="0pt"
android:layout_height="match_parent"
android:text="@string/button_pin2"
android:background="@drawable/button_green"
android:layout_margin="2dp"
android:drawableBottom="@mipmap/ic_arrow_right" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0pt"
android:orientation="horizontal">
<Button
android:id="@+id/button3"
android:layout_weight="1"
android:layout_width="0pt"
android:layout_height="match_parent"
android:text="@string/button_pin3"
android:background="@drawable/button_yellow"
android:layout_margin="2dp"
android:drawableBottom="@mipmap/ic_arrow_up"/>
<Button
android:id="@+id/button4"
android:layout_weight="1"
android:layout_width="0pt"
android:layout_height="match_parent"
android:text="@string/button_pin4"
android:background="@drawable/button_purple"
android:layout_margin="2dp"
android:drawableBottom="@mipmap/ic_arrow_down"/>
</LinearLayout>
感谢您的回答。
编辑(jaydroider):
我已经尝试过你的方法,但这就是我得到的updated image。 在 android studio 中,我查看了不同的屏幕尺寸,这段代码不会填满整个屏幕。我不知道我是不是在做坏事。
这是我更新的代码:
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.jigsik.arduinocontrol.Pin8aActivity">
<LinearLayout style="@style/CoreLayout">
<TextView
android:id="@+id/myText"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout style="@style/CoreLayout">
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/stop_all"
android:onClick="stopAll"
android:background="@drawable/button_red"
android:layout_margin="2dp"
android:drawableBottom="@mipmap/ic_stop"/>
</LinearLayout>
<LinearLayout style="@style/CoreLayout">
<Button
android:id="@+id/button1"
style="@style/Button.Blue"
android:text="@string/button_pin1"
android:drawableBottom="@mipmap/ic_arrow_left" />
<Button
android:id="@+id/button2"
style="@style/Button.Green"
android:text="@string/button_pin2"
android:drawableBottom="@mipmap/ic_arrow_right"/>
</LinearLayout>
<LinearLayout style="@style/CoreLayout">
<Button
android:id="@+id/button3"
style="@style/Button.Yellow"
android:text="@string/button_pin3"
android:drawableBottom="@mipmap/ic_arrow_up"/>
<Button
android:id="@+id/button4"
style="@style/Button.Purple"
android:text="@string/button_pin4"
android:drawableBottom="@mipmap/ic_arrow_down"/>
</LinearLayout>
<LinearLayout style="@style/CoreLayout">
<Button
android:id="@+id/button5"
style="@style/Button.Orange"
android:text="@string/button_pin5"
android:drawableBottom="@mipmap/ic_arrow_right"/>
<Button
android:id="@+id/button6"
style="@style/Button.Azure"
android:text="@string/button_pin6"
android:drawableBottom="@mipmap/ic_arrow_left"/>
</LinearLayout>
<LinearLayout style="@style/CoreLayout">
<Button
android:id="@+id/button7"
style="@style/Button.Pink"
android:text="@string/button_pin7"
android:drawableBottom="@mipmap/ic_arrow_down"/>
<Button
android:id="@+id/button8"
style="@style/Button.Grey"
android:text="@string/button_pin8"
android:drawableBottom="@mipmap/ic_arrow_up"/>
</LinearLayout>
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="CoreLayout">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:orientation">horizontal</item>
<item name="android:weightSum">2</item>
</style>
<style name="Button">
<item name="android:layout_weight">1</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">2dp</item>
<item name="android:layout_gravity">center</item>
<item name="android:gravity">center</item>
<item name="android:padding">10dp</item>
</style>
<style name="Button.Blue">
<item name="android:background">@drawable/button_blue</item>
</style>
<style name="Button.Green">
<item name="android:background">@drawable/button_green</item>
</style>
<style name="Button.Yellow">
<item name="android:background">@drawable/button_yellow</item>
</style>
<style name="Button.Purple">
<item name="android:background">@drawable/button_purple</item>
</style>
<style name="Button.Orange">
<item name="android:background">@drawable/button_orange</item>
</style>
<style name="Button.Azure">
<item name="android:background">@drawable/button_azure</item>
</style>
<style name="Button.Pink">
<item name="android:background">@drawable/button_pink</item>
</style>
<style name="Button.Grey">
<item name="android:background">@drawable/button_grey</item>
</style>
编辑:我认为这种情况没有我想要的解决方案。在我看来,只有一种方法可以完成这项任务。我必须为较小的屏幕制作第二个布局文件并对其进行更改以实现所需的行为(较小的图像等)。谢谢你们的回答。如果不与您进行头脑风暴,我将找不到解决方案:-)。
【问题讨论】:
-
您必须为任何分辨率设计布局,请参阅我的回答here
-
@mehrdadkhosravi 我看过你的回答。你想说我必须为每种手机制作 5 种不同版本的布局?我希望有另一种方法,因为我必须制作更多具有不同按钮数的应用程序版本。在每种情况下都感谢您的回答!
-
您可以设计 5 种不同版本的布局,但这不是一个好的解决方案。您可以将 5 个维度用于设计 1 个布局,用于 5 种不同的分辨率。你知道dimens吗?
标签: android xml android-layout button android-linearlayout