【问题标题】:Android: Drawablebottom hides textAndroid:Drawablebottom隐藏文本
【发布时间】:2016-05-11 10:06:24
【问题描述】:

我有几个线性布局(见底部的图片和一段代码)。每个按钮上都有一个文本,文本下有一个图像。但是有一个问题。当应用程序在分辨率较低的手机上运行时,图像会隐藏文本。如何防止这种情况发生?

我将 layout_height 设置为 0pt 以让 layout_weight 处理这个问题。

image of layout

<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


【解决方案1】:

android:weightSum="2" 给您的每个Linear Layout。我已将更改应用到您的 Layout。即使它适用于较小的Layouts

参考这个。

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">


        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableBottom="@mipmap/ic_launcher"
            android:gravity="center"
            android:padding="20dp"
            android:text="Pin1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableBottom="@mipmap/ic_launcher"
            android:gravity="center"
            android:padding="20dp"
            android:text="Pin2" />
    </LinearLayout>

EDIT1:

超过 10 个Buttons。不要为每个Linear Layout 使用style

请参考。

<?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">

    <LinearLayout
        android:id="@+id/LL1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">


        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableBottom="@mipmap/ic_launcher"
            android:gravity="center"
            android:padding="20dp"
            android:text="Pin1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableBottom="@mipmap/ic_launcher"
            android:gravity="center"
            android:padding="20dp"
            android:text="Pin2" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LL2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">


        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableBottom="@mipmap/ic_launcher"
            android:gravity="center"
            android:padding="20dp"
            android:text="Pin3" />

        <Button
            android:id="@+id/button4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableBottom="@mipmap/ic_launcher"
            android:gravity="center"
            android:padding="20dp"
            android:text="Pin4" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LL3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">


        <Button
            android:id="@+id/button5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableBottom="@mipmap/ic_launcher"
            android:gravity="center"
            android:padding="20dp"
            android:text="Pin5" />

        <Button
            android:id="@+id/button6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableBottom="@mipmap/ic_launcher"
            android:gravity="center"
            android:padding="20dp"
            android:text="Pin6" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LL4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">


        <Button
            android:id="@+id/button7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableBottom="@mipmap/ic_launcher"
            android:gravity="center"
            android:padding="20dp"
            android:text="Pin7" />

        <Button
            android:id="@+id/button8"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableBottom="@mipmap/ic_launcher"
            android:gravity="center"
            android:padding="20dp"
            android:text="Pin8" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LL5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">


        <Button
            android:id="@+id/button9"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableBottom="@mipmap/ic_launcher"
            android:gravity="center"
            android:padding="20dp"
            android:text="Pin9" />

        <Button
            android:id="@+id/button10"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableBottom="@mipmap/ic_launcher"
            android:gravity="center"
            android:padding="20dp"
            android:text="Pin10" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LL6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">


        <Button
            android:id="@+id/button11"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableBottom="@mipmap/ic_launcher"
            android:gravity="center"
            android:padding="20dp"
            android:text="Pin11" />

        <Button
            android:id="@+id/button12"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableBottom="@mipmap/ic_launcher"
            android:gravity="center"
            android:padding="20dp"
            android:text="Pin12" />
    </LinearLayout>

</LinearLayout>

这是屏幕。

【讨论】:

  • @MartinPohorský 为什么要为每个Linear Layout 提供核心布局样式。尝试我在回答中给出的内容。不要在布局的根标签中使用样式。可能是导致问题。使用我在回答中给出的相同。它适用于所有小型、大型、中型大型设备。
  • 我已按照您的“将 android:weightSum="2" 分配给您的每个线性布局”。我不想有一个大问题,所以我只发布了一个线性布局。如果我将您的方法用于 f.e. 10 个按钮而不是 8 个,我在三星 Galaxy Note 4 上遇到了同样的情况。您是否尝试过使用多个按钮的解决方案?
  • @MartinPohorský 是的,我已经尝试过在我身边使用更多按钮。这是因为答案没有变大,所以我只放了一个线性布局。'
  • 我不明白我做错了什么。您能否向我提供您的(完整)代码以及更多按钮(比如说 10 个或更多)?谢谢。
  • @MartinPohorský 我将提供,但您只需复制并粘贴 Single Linear Layout with Two Buttons 复制任意多次,然后您就可以使用 10 个或更多按钮获得该输出。
【解决方案2】:

为此,

你需要制作不同尺寸的图片检查http://developer.android.com/guide/practices/screens_support.html

另一件事是你必须将 LinearLayout 添加为 parent 到其他 LinearLayout,检查下面的代码

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="2" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:background="@drawable/cal"
            android:drawableBottom="@mipmap/ic_launcher"
            android:text="button_pin1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0pt"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:background="@drawable/cal"
            android:drawableBottom="@mipmap/ic_launcher"
            android:text="button_pin2" />
    </LinearLayout >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:background="@drawable/cal"
            android:drawableBottom="@mipmap/ic_launcher"
            android:text="button_pin3" />

        <Button
            android:id="@+id/button4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:background="@drawable/cal"
            android:drawableBottom="@mipmap/ic_launcher"
            android:text="button_pin4" />
    </LinearLayout >
</LinearLayout >

现在,根据您的要求,您可以更改 weightSum 并添加 LinearLayout

【讨论】:

  • 我以错误的方式发布了我的问题。我有父线性布局。我添加了 weightSum。我对 LinearLayouts 没有问题。布局刚刚好。我只有用图像隐藏文本的问题。该按钮具有固定大小,当图像不能更小时,它不会关心是否有文本。我在 mdpi、hdpi、xhdpi、xxhdpi、xxxhdpi 中有这些图像。我看到还有ldpi,但是Android studio不让我有。
  • 恐怕我想让这些图像太小。但是如果我需要 f.e. 16 个按钮?
  • 如果有16个按钮,你可以根据分辨率使用scrollview和图像。
  • 是的。这将是一个解决方案。但我不能在这个项目中使用滚动视图。请参阅@N.T. 的评论。回答
【解决方案3】:

无论您将图像制作得多么小,在某些时候,如果您添加的按钮足够多,它们将无法在屏幕上显示。

可以接受滚动吗?

您可以将您发布的第二个布局完全照原样嵌入到ScrollView 中。这样您就可以拥有所需大小的按钮 - 如果恰好有太多按钮,您可以滚动以找到额外的按钮。

【讨论】:

  • 我明白了。不幸的是,这不是我可以接受的解决方案。该应用程序最多只能使用残疾人(他们的手指也可能有问题,滚动屏幕可能会使应用程序难以控制)。但我同意你的观点,也认为图像不能转换为超小。只有 5 种尺寸的图像可用。但我有个主意。我不能自己以编程方式转换图像吗?我知道它会消耗硬件,但应用程序并没有那么复杂。
  • 一段时间后,我认为这是最好的答案,因为无法始终将图片与文本一起放入按钮中。我使用没有文字的图像解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
  • 2011-02-17
相关资源
最近更新 更多