android:id="@+id/bSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="Search"
android:textSize="24sp" />
我试图创建一个带有文字和图标的按钮。
android:drawableLeft对我不起作用(也许可以,但我不知道如何给图标设置一个最大高度)。
所以我创建了一个带有ImageView和TextView的LinearLayout,并让它像一个按钮一样行动。
<LinearLayout
android:id="@+id/bSearch2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_default"
android:clickable="true"
android:padding="16dp"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:adjustViewBounds="true"
android:maxHeight="30dp"
android:maxWidth="30dp"
android:scaleType="fitCenter"
android:src="@drawable/search_icon" />
<TextView
android:id="@+id/tvSearchCaption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="24sp"
android:paddingRight="30dp"
android:gravity="center"
android:text="Search" />
</LinearLayout>
我的新按钮正是我想要的(字体大小、图标和文字位置)。
但它看起来不象我的默认按钮。
所以我试图改变我的新Button的背景和文本颜色。
Button Search = (Button) findViewById(R.id.bSearch);
LinearLayout bSearch2 = (LinearLayout) findViewById(R.id.bSearch2);
bSearch2.setBackground(bSearch.getBackground());
TextView tvSearchCaption = (TextView)findViewById(R.id.tvSearchCaption);
tvSearchCaption.setTextColor(bSearch.getTextColors().getDefaultColor());
这产生了一个奇怪的结果,我的旧按钮,被打乱了。
当我改变这两个按钮在XML中的顺序,让 "新按钮 "放在前面时,会产生另一个奇怪的结果。
现在我注意到,当我试图按下旧的按钮时,新的按钮被按下。
有什么想法吗?
9 个回答
0 人赞同
Try this one.
<Button
android:id="@+id/bSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="Search"
android:drawableLeft="@android:drawable/ic_menu_search"
android:textSize="24sp"/>
0 人赞同
要将图像添加到左边、右边、上面或下面,你可以使用这样的属性。
android:drawableLeft
android:drawableRight
android:drawableTop
android:drawableBottom
上面给出了示例代码。你也可以使用相对布局来实现这一点。
0 人赞同
你可以使用材料组件库和MaterialButton
组件。
Use the app:icon
和app:iconGravity="start"
属性。
Something like:
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.Icon"
app:icon="@drawable/..."
app:iconGravity="start"
0 人赞同
对于任何想动态地做这件事的人来说,按钮对象上的setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)
会有帮助。
Button search = (Button) findViewById(R.id.yoursearchbutton);
search.setCompoundDrawablesWithIntrinsicBounds('your_drawable',null,null,null);
0 人赞同
这就是你真正想要的东西。
<Button
android:id="@+id/settings"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/colorAccent"
android:drawableStart="@drawable/ic_settings_black_24dp"
android:paddingStart="40dp"
android:paddingEnd="40dp"
android:text="settings"
android:textColor="#FFF" />
0 人赞同
如果你使用的是android.widget.Button而没有进行任何覆盖,那么@Liem Vo的答案是正确的。如果你正在使用MaterialComponents覆盖你的主题,这将不能解决这个问题。
因此,如果你是
Using com.google.android.material.button.MaterialButton
Overriding AppTheme using MaterialComponents
Use 应用程序:图标参数。
<Button
android:id="@+id/bSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="Search"
android:textSize="24sp"
app:icon="@android:drawable/ic_menu_search" />
0 人赞同
像这样呢?
<Button
android:id="@+id/yourID"
android:drawableStart="@drawable/ic_flag"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:textStyle="bold"
android:textAlignment="textStart"
android:background="@drawable/btn_background"
android:fontFamily="@font/YourFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Text" />
0 人赞同
try It:-
position-> left,right,top,bottom
android:drawableposition="@android:drawable/-yourImage-"
android:drawabletop="-Yourfilesrc-"
android:drawablebottom="-Yourfilesrc-"
android:drawableleft="-Yourfilesrc-"
android:drawableright="-Yourfilesrc-"
0 人赞同
我在纯Button视图中也遇到了同样的问题。在我的情况下,XML的解决方案是没有用的,因为我有一些逻辑来显示/隐藏这个图标。
There is one more solution using pure Kotlin:
(bSearch as? MaterialButton)?.let {