Android:Drawablebottom隐藏文字

时间:2016-05-11 10:06:25

标签: android xml android-layout button android-linearlayout

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

我将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工作室我看了不同的屏幕尺寸,这个代码不会填满整个屏幕。我不知道我是不是做了坏事。

以下是我更新的代码:

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>

编辑:我认为这种情况没有我想要的解决方案。在我看来,只有一种方法可以完成这项任务。我必须为较小的屏幕制作第二个布局文件并更改它以实现所需的行为(较小的图像等)。谢谢你的回答。如果没有头脑风暴,我就找不到解决办法: - )。

3 个答案:

答案 0 :(得分:2)

  

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。不要为style Linear Layout使用<?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>

请参阅此内容。

fscanf(ifile, "%[^,] %*c %[^,] %*c %d %*c %d %*c %f", first[0], last[0], num, human, cool);

这是屏幕。

enter image description here

答案 1 :(得分:1)

为此,

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

另外一件事是您必须将 LinearLayout 添加为到其他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

答案 2 :(得分:0)

无论你制作的图像有多小,在某些时候,如果你添加足够多的按钮,它们就不适合屏幕。

滚动是否可以接受?

您可以将第二个发布的布局与其完全相同,并将其嵌入ScrollView。这样你就可以得到你想要的尺寸 - 如果碰巧有太多的按钮,你可以滚动来达到额外的按钮。

相关问题