【问题标题】:Customizing my button自定义我的按钮
【发布时间】:2016-08-10 04:39:51
【问题描述】:

我有带有文本的按钮。我想要的是没有文字的同一个按钮,按钮中间有一个小图标。我假设该图标必须是您保存在可绘制文件中的图像,但是当我将其放入按钮中时,它会被设置为背景..

此外,所有带有图标的图像都是带有黑色图标的图像,并被空白包围以使其成为正方形。我想保留我的按钮,只是在它的中心有一个图标。 有谁知道如何做到这一点?

活动主代码:

<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.example.rodekruis.MainActivity">


    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.rodekruis.MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:src="@drawable/rkz_logo"
        android:layout_gravity="center" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginBottom="20dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button10"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_alignTop="@+id/button8"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="  Afspraak   maken" />


        <Button
            android:id="@+id/button8"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="  Bezoek  tijden" />

        <Button
            android:id="@+id/button9"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:background="@drawable/contact"
            android:text="Contact" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="20dp"
        android:orientation="horizontal">


        <Button
            android:id="@+id/button3"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text=" Geef je mening!" />


        <Button
            android:id="@+id/button4"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text=" Route begeleiding" />

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_alignLeft="@+id/button5"
            android:layout_below="@+id/button8"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="Specialisten" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button5"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:layout_weight="1.03"
            android:text="Brandwonden centrum" />


        <Button
            android:id="@+id/button6"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="Agenda" />


        <Button
            android:id="@+id/button7"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="Nieuws" />
    </LinearLayout>

</LinearLayout>
</LinearLayout>

这是顶行按钮的代码:

 <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.rodekruis.MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:src="@drawable/rkz_logo"
        android:layout_gravity="center" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginBottom="20dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button10"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_alignTop="@+id/button8"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="  Afspraak   maken" />


        <Button
            android:id="@+id/button8"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="  Bezoek  tijden" />

        <ImageButton
            android:id="@+id/button9"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:src="@drawable/rkz_logo"
            android:gravity="center"
            android:text="Contact" />

    </LinearLayout>

【问题讨论】:

标签: android eclipse layout


【解决方案1】:

使用ImageButton 小部件而不是Button 小部件。然后使用android:src标签设置图片,android:gravity="center"设置图片重心为中心。

希望它会有所帮助:)

更新 然后试试这个 -

 <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"  //Your image icon
        android:background="@android:drawable/btn_default_small"/>

【讨论】:

  • 感谢您的回复。这将删除普通按钮,并将图像用作按钮。我想保留普通按钮,并在那个普通按钮中保留另一个小图像。所以你仍然有正方形作为按钮,而不是图像。
  • 好的。然后试试这个 -
  • 快到了。图像在按钮上方,按钮仍然存在,这就是我想要的。问题是,图像太大,所以它会越过它并使按钮比它想象的要大。甚至来自谷歌的图标图像也太大了。我可以在代码中更改它的大小吗?
  • 不..你不能..但你可以设置小图像..使用Photoshop等工具创建PNG图标。与差异。大小,然后将该图标放入相应的 mipmap 文件夹中。而已!希望它会有所帮助:)
【解决方案2】:

试试ImageButton。顾名思义,它是一个带有图像的按钮。要设置图像,请执行以下操作:

android:src

希望这会有所帮助。

【讨论】:

  • 这会删除普通按钮,并将图像用作按钮。我想保留普通按钮,并在那个普通按钮中保留另一个小图像。所以你仍然有正方形作为按钮,而不是图像。
【解决方案3】:

改变

<Button
            android:id="@+id/button9"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:background="@drawable/contact"
            android:text="Contact" />

<ImageButton
            android:id="@+id/button9"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:src="@drawable/contact"
            android:text="Contact" />

如果你不想方形按钮编辑

<ImageButton
                android:id="@+id/button9"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="5dp"
                android:background="@android:color/transparent"
                android:layout_weight="1"
                android:src="@drawable/contact"
                android:text="Contact" />

【讨论】:

  • 这会删除普通按钮,并将图像用作按钮。我想保留普通按钮,并在那个普通按钮中保留另一个小图像。所以你仍然有正方形作为按钮,而不是图像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 2013-05-25
  • 1970-01-01
  • 2017-11-14
  • 2016-08-08
  • 2013-09-20
  • 2015-08-23
相关资源
最近更新 更多