Android 带文本的可绘制左中
我无法将我的drawableLeft图标居中。Android 带文本的可绘制左中,android,android-layout,Android,Android Layout,我无法将我的drawableLeft图标居中。 我可以很容易地将图标放在文本的左侧,但如果我将“重力”设置为“中心”,则只有文本居中,而没有图标 <Button android:id="@+id/patient_list_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/patientList" a
我可以很容易地将图标放在文本的左侧,但如果我将“重力”设置为“中心”,则只有文本居中,而没有图标
<Button
android:id="@+id/patient_list_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/patientList"
android:drawableLeft="@drawable/ic_icon_two_heads"
android:layout_marginBottom="15dp"
style="@style/darkBlueButtonWithImage"/>
这就是我想要的:这就是我所拥有的:
10dp
@颜色/白色
10dp
@可拉拔/半径\深\蓝\按钮
中心垂直|左
@颜色/白色
我怎样才能做到这一点呢?在尝试了谷歌的方法9000多次之后,我几乎总是使用一个视图组将两者并排放置,特别是
相对视图或线性布局,然后添加一个ImageView和一个TextView。这很蹩脚,但是drawableStart/End有太多缺少的特性,以至于在你意识到“你做不到”之前,你会浪费很多时间
对齐、着色、矢量等。所有这些都是“内置”可绘制的更难或不可能的 使用按钮
属性android:drawableLeft
和android:drawablePadding
将无法获得预期结果。您可以使用RelativeLayout
或LinearLayout
、TextView
和ImageView
创建自定义按钮。使用
定义按钮状态(正常/按下的)行为
下面是一个工作示例。试试这个:
<!--- Custom Button -->
<RelativeLayout
android:id="@+id/patient_list_button"
android:layout_width="match_parent"
android:layout_height="48dp"
android:clickable="true"
android:background="@drawable/custom_button_selector">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true"
android:gravity="center_vertical">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/icon_refresh"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:textSize="16dp"
android:text="CUSTOM BUTTON"
android:textStyle="bold"
android:textColor="#FFFFFF"/>
</LinearLayout>
</RelativeLayout>
自定义按钮选择器.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/bg_custom_button_pressed" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/bg_custom_button_pressed" />
<item
android:state_enabled="true"
android:drawable="@drawable/bg_custom_button_normal" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- view background color -->
<solid
android:color="#01A1DD" >
</solid>
<!-- Here is the corner radius -->
<corners android:radius="8dp" >
</corners>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- view background color -->
<solid
android:color="#303F9F">
</solid>
<!-- Here is the corner radius -->
<corners android:radius="8dp" >
</corners>
</shape>
bg\u自定义按钮\u normal.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/bg_custom_button_pressed" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/bg_custom_button_pressed" />
<item
android:state_enabled="true"
android:drawable="@drawable/bg_custom_button_normal" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- view background color -->
<solid
android:color="#01A1DD" >
</solid>
<!-- Here is the corner radius -->
<corners android:radius="8dp" >
</corners>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- view background color -->
<solid
android:color="#303F9F">
</solid>
<!-- Here is the corner radius -->
<corners android:radius="8dp" >
</corners>
</shape>
bg\u自定义按钮\u按下。xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/bg_custom_button_pressed" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/bg_custom_button_pressed" />
<item
android:state_enabled="true"
android:drawable="@drawable/bg_custom_button_normal" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- view background color -->
<solid
android:color="#01A1DD" >
</solid>
<!-- Here is the corner radius -->
<corners android:radius="8dp" >
</corners>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- view background color -->
<solid
android:color="#303F9F">
</solid>
<!-- Here is the corner radius -->
<corners android:radius="8dp" >
</corners>
</shape>
输出:
<!--- Custom Button -->
<RelativeLayout
android:id="@+id/patient_list_button"
android:layout_width="match_parent"
android:layout_height="48dp"
android:clickable="true"
android:background="@drawable/custom_button_selector">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true"
android:gravity="center_vertical">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/icon_refresh"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:textSize="16dp"
android:text="CUSTOM BUTTON"
android:textStyle="bold"
android:textColor="#FFFFFF"/>
</LinearLayout>
</RelativeLayout>
希望这将有助于~a)增加“paddingLeft”值并缩小或删除“drawablePadding”。在您的情况下,请将该值调整为合适的值;例如:
android:paddingLeft="50dp"
android:gravity="center_vertical|left"
b) 使用自定义视图。最后,经过这么多年,我们有可能以简单直观的方式实现这种行为。
你所要做的就是使用谷歌材料库。然后使用带有图标的样式。之后,您可以使用app:iconGravity
属性并将其设置为textStart
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.Icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some text"
app:iconGravity="textStart"
app:icon="@drawable/some_icon" />
IMO最简单的解决方案:
yourButtonView.text = SpannableString(" ${getString(R.string.your_string)}").apply {
setSpan(
ImageSpan(requireContext(), R.drawable.your_icon),
0,
1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
有点黑吗?大概它是否适用于每个按钮(MaterialButton、AppCompatButton、button等)?是的
我更喜欢这样做,而不是创建自定义视图之类的麻烦事。使用drawablepadding@JanardhanR如果按钮的文本是动态的,则使用填充将不可能。在样式中,重力设置为center\u vertical | left
。尝试将其替换为center
makecustomview而不是按钮@Jaydeppatel真的……您将为此创建一个自定义视图。@MarekCzaplicki,我的荣幸:)。您可以使用Android studio的“图像资源”
选项获取图标。只需右键单击任意项目“文件>新建>图像资源”
。谢谢~@Marek:我可以知道拒绝接受这个答案的原因吗?thx,在我们的项目中,我们有一个自定义按钮,它可以继承MaterialButton,使用ImageView和TextView进行新布局都不是一个选项(好吧,模仿我们当前所有的电视按钮行为非常耗时和费钱)。您的解决方案(或黑客:)运行良好,解决了问题-非常感谢:)好的,再给谷歌一次机会,就最后一次,哦,不。