【问题标题】:Rounded corners in Android ImageButton doesn't workAndroid ImageButton 中的圆角不起作用
【发布时间】:2019-02-23 06:23:15
【问题描述】:

我的代码有问题吗? round_corner.xml(我把它放在drawable中)

  <?xml version="1.0" encoding="utf-8"?>

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="#FFFF0F" />
    <corners android:radius="30dp" />
    </shape>

我认为我的 imageButton 中的代码不起作用

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <ImageButton
        android:id="@+id/my_button"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:layout_gravity="center"
        android:background="@drawable/round_corner"
        android:src="@drawable/my_pic"/>

    </LinearLayout>

它没有给我任何错误。它只是没有让我的形象变得圆润。有什么想法吗?

【问题讨论】:

标签: android xml image button


【解决方案1】:

我建议你使用CardView 而不是图片按钮

你可以通过这段代码CardView达到同样的效果。

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/my_pic"
        app:cardCornerRadius="30dp"/>

【讨论】:

    【解决方案2】:

    首先创建一个shape.xml

        <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
        <solid android:color="#FFFF0F" />
        <corners android:radius="30dp" />
        </shape>

    使用这个xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
    
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_gravity="center"
    
        android:layout_margin="8dp"
        android:background="@drawable/shape"
    
        android:elevation="4dp">
    
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:elevation="4dp"
            app:cardCornerRadius="8dp">
    
            <ImageButton
                android:id="@+id/thumbnaill"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="left"
                android:background="@drawable/maxresdefault"
                android:scaleType="fitXY"
                />
        </android.support.v7.widget.CardView>
    </LinearLayout>

    这是你的输出:) 请注意它不会在 Xml 预览中显示半径运行它会出现的应用程序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多