【问题标题】:Design a button, android?设计一个按钮,android?
【发布时间】:2014-08-20 10:34:50
【问题描述】:

我需要设计一个这种类型的按钮,它应该有一个背景,中间是另一个小图像,底部是文本。

虽然我当前使用的是如下文本视图

     <TextView
            android:id="@+id/homeButton1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:gravity="center"
            android:text="Button"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@drawable/button_color"
            android:textStyle="bold" />

但是我不能把图像放在它的中心。

如果我使用 android:drawableTop 这是我得到的图像

这怎么可能?

【问题讨论】:

  • 使用imagebutton和textview对齐bottem到imagebutton
  • 你可以使用复合drawables
  • 在下面查看我的答案。

标签: android xml button textview


【解决方案1】:

试试复合drawables这里是一个例子

<TextView
         android:id="@+id/image"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:drawableTop="@drawable/image"
         android:gravity="center"
         android:text="@string/text" />

输出:

【讨论】:

  • @Shink 检查我的答案
【解决方案2】:

在您的 TextView xml 中添加一个波纹管并将“img_src”更改为您的图像。

android:drawableTop="@drawable/img_src"

更新 1:

drawableTop只支持在文字上方绘制。

如果你想在中心绘制图像,这里有一个示例代码。

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/background_image" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/center_image" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:text="TextView" />

</RelativeLayout>

【讨论】:

  • drawableTop 不使图像居中。
  • @Shink 在下面查看我的答案。
【解决方案3】:

检查这个,完全按照你的意愿工作

<Button
        android:id="@+id/btn"
        style="@style/MyButton"
        android:drawableTop="@drawable/btn_my"
        android:text="SomeText" />

btn_my.xml // 将其添加到可绘制文件夹中

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_launcher"
        android:state_focused="true"
        android:state_pressed="true" />
    <item android:drawable="@drawable/ic_launcher"
        android:state_focused="false"
        android:state_pressed="true" />
    <item android:drawable="@drawable/ic_launcher" android:state_focused="true" />
    <item android:drawable="@drawable/ic_launcher"
        android:state_focused="false"
        android:state_pressed="false" />
</selector>

将以下内容复制到styles.xml中

<style name="MyButton">
        <item name="android:layout_gravity">center_vertical</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:drawablePadding">2dp</item>
        <item name="android:textSize">16dp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">#ff29549f</item>
        <item name="android:background">@null</item>
    </style>    

这是输出

【讨论】:

    猜你喜欢
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 2015-01-04
    • 1970-01-01
    相关资源
    最近更新 更多