【问题标题】:How can I show the text on a ImageButton?如何在 ImageButton 上显示文本?
【发布时间】:2019-10-08 06:34:35
【问题描述】:

我想为我的应用创建这种类型的布局,有人可以帮我吗。我怎样才能创建这种类型的布局?

Layout screen shot ,please check it

【问题讨论】:

  • 使用框架布局。背面的图像和顶部的按钮。设置按钮可点击:false 所以你的图片会收到点击

标签: android android-layout android-linearlayout


【解决方案1】:

对于一个按钮,这也很好用。放 android:background 就好了!

<Button
android:id="@+id/fragment_left_menu_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_bg"
android:text="@string/login_string" />

【讨论】:

    【解决方案2】:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    
        android:gravity="center"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_marginTop="5dp"
            android:layout_weight="2"
            android:background="@drawable/images"
            android:gravity="center">
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/style"
                android:text="Pasta"
                android:textColor="#ffffff" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_marginTop="5dp"
            android:layout_weight="2"
            android:background="@drawable/images"
            android:gravity="center">
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/style"
                android:text="Pasta"
                android:textColor="#ffffff" />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_marginTop="5dp"
            android:layout_weight="2"
            android:background="@drawable/images"
            android:gravity="center">
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/style"
                android:text="Pasta"
                android:textColor="#ffffff" />
    
        </LinearLayout>
    </LinearLayout>
    

    在你的drawable中创建一个样式文件

    <?xml version="1.0" encoding="UTF-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android">
        <stroke
            android:width="1dp"
            android:color="#FFFFFF"/>
        <corners
            android:radius="7dp" />
    
        <padding
            android:left="1dp"
            android:right="1dp"
            android:top="1dp"
            android:bottom="1dp"/>
    
        <solid android:color="#55ffffff"/>
    
    </shape>
    

    【讨论】:

    • 非常感谢它正在工作,但是我如何在图像之间添加空间。?
    • 可以增加每个LinearLayout的android:layout_marginTop。
    • 如果我的回复回答了您的问题,如果您接受我的回复,我会很高兴。
    【解决方案3】:

    在这种情况下,您可以使用 MaterialButton。

    <com.google.android.material.button.MaterialButton
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:text="Non vegetarian"
        android:textColor="@color/colorPrimary"
        android:textAllCaps="false"
        android:background="@drawable/bg_rounded_btn"
        app:backgroundTint="@drawable/bg_rounded_btn" />
    

    @drawable/bg_rounded_btn 文件:

        <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="rectangle">
    
           <corners android:radius="10dp" />
    
           // In your case color transparent
           <solid android:color="@color/white" />
    
           // In your case color white
           <stroke
               android:width="1dp"
               android:color="@color/scooter_dark" />
    
       </shape>
    

    result

    【讨论】:

      猜你喜欢
      • 2012-02-21
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      相关资源
      最近更新 更多