如何在宽度设置为“填充父项”的androidbutton中居中放置图标和文本

我想要一个带有图标+文本的Androidbutton。 我使用drawableLeft属性来设置图像,如果button的宽度是"wrap_content"但是我需要拉伸到最大宽度,所以我使用宽度"fill_parent" 。 这将我的图标直接移动到button的左侧,我希望图标和文本都位于button的中心。

我尝试设置填充,但这只允许给一个固定值,所以它不是我所需要的。 我需要在中心alignment图标+文本。

 <Button android:id="@+id/startTelemoteButton" android:text="@string/start_telemote" android:drawableLeft="@drawable/start" android:paddingLeft="20dip" android:paddingRight="20dip" android:width="fill_parent" android:heigh="wrap_content" /> 

任何build议,我怎么能实现呢?

android:drawableLeft始终保持android:paddingLeft距左边界的距离。 当button没有设置为android:width =“wrap_content”时,它会一直挂在左边!

使用Android 4.0(API等级14),您可以使用android:drawableStart属性在文本的开头放置一个drawable。 我唯一向下兼容的解决scheme是使用ImageSpan创build一个文本+图像Spannable:

 Button button = (Button) findViewById(R.id.button); Spannable buttonLabel = new SpannableString(" Button Text"); buttonLabel.setSpan(new ImageSpan(getApplicationContext(), R.drawable.icon, ImageSpan.ALIGN_BOTTOM), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); button.setText(buttonLabel); 

在我的情况下,我还需要调整Button的android:gravity属性,使其看起来居中:

 <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minHeight="32dp" android:minWidth="150dp" android:gravity="center_horizontal|top" /> 

我知道我在回答这个问题上迟了一些,但是这帮助了我:

 <FrameLayout android:layout_width="match_parent" android:layout_height="35dp" android:layout_marginBottom="5dp" android:layout_marginTop="10dp" android:background="@color/fb" > <Button android:id="@+id/fbLogin" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center" android:background="@null" android:drawableLeft="@drawable/ic_facebook" android:gravity="center" android:minHeight="0dp" android:minWidth="0dp" android:text="FACEBOOK" android:textColor="@android:color/white" /> </FrameLayout> 

我从这里find了这个解决scheme: Android UI挣扎:制作一个中心文字和图标的button

在这里输入图像说明

我使用LinearLayout而不是ButtonOnClickListener ,我需要使用也适用于LinearLayout。

 <LinearLayout android:id="@+id/my_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/selector" android:gravity="center" android:orientation="horizontal" android:clickable="true"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="15dp" android:adjustViewBounds="true" android:scaleType="fitCenter" android:src="@drawable/icon" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="Text" /> </LinearLayout> 

你可以使用自定义button来测量和绘制自己以容纳左边的drawable。 请在这里find例子和用法

 public class DrawableAlignedButton extends Button { public DrawableAlignedButton(Context context, AttributeSet attrs) { super(context, attrs); } public DrawableAlignedButton(Context context) { super(context); } public DrawableAlignedButton(Context context, AttributeSet attrs, int style) { super(context, attrs, style); } private Drawable mLeftDrawable; @Override //Overriden to work only with a left drawable. public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) { if(left == null) return; left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight()); mLeftDrawable = left; } @Override protected void onDraw(Canvas canvas) { //transform the canvas so we can draw both image and text at center. canvas.save(); canvas.translate(2+mLeftDrawable.getIntrinsicWidth()/2, 0); super.onDraw(canvas); canvas.restore(); canvas.save(); int widthOfText = (int)getPaint().measureText(getText().toString()); int left = (getWidth()+widthOfText)/2 - mLeftDrawable.getIntrinsicWidth() - 2; canvas.translate(left, (getHeight()-mLeftDrawable.getIntrinsicHeight())/2); mLeftDrawable.draw(canvas); canvas.restore(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int height = getMeasuredHeight(); height = Math.max(height, mLeftDrawable.getIntrinsicHeight() + getPaddingTop() + getPaddingBottom()); setMeasuredDimension(getMeasuredWidth(), height); } } 

用法

 <com.mypackage.DrawableAlignedButton android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:drawableLeft="@drawable/my_drawable" android:gravity="center" android:padding="7dp" android:text="My Text" /> 

我最近遇到了同样的问题。 试图find一个更便宜的解决scheme,所以想出了这个。

  <LinearLayout android:id="@+id/linearButton" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/selector_button_translucent_ab_color" android:clickable="true" android:descendantFocusability="blocksDescendants" android:gravity="center" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/app_name" android:src="@drawable/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textColor="@android:color/white" /> </LinearLayout> 

然后调用LinearLayout上的OnClickListener

希望这可以帮助某人,因为这似乎是一个非常普遍的问题。 🙂

我知道这个问题是有点老,但也许你仍然打开提示或解决方法:

使用button图像作为背景创build一个RelativeLayout“wrap_content”,或者将button本身作为布局的第一个元素。 获取LinearLayout并将其设置为“layout_centerInParent”和“wrap_content”。 然后将您的Drawable设置为Imageview。 最后用你的文本(locale)设置一个TextView。

所以基本上你有这样的结构:

 RelativeLayout Button LinearLayout ImageView TextView 

或者像这样:

 RelativeLayout with "android-specific" button image as background LinearLayout ImageView TextView 

我知道这个解决scheme有许多要处理的内容,但是你可以用它创build非常容易的自定义button,还可以设置文本和可绘制的确切位置:)

我通过左右添加填充来调整它,如下所示:

 <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/btn_facebookact_like" android:text="@string/btn_facebookact_like" android:textColor="@color/black" android:textAllCaps="false" android:background="@color/white" android:drawableStart="@drawable/like" android:drawableLeft="@drawable/like" android:gravity="center" android:layout_gravity="center" android:paddingLeft="40dp" android:paddingRight="40dp" /> 

您可以创build一个自定义小部件:

Java类IButton:

 public class IButton extends RelativeLayout { private RelativeLayout layout; private ImageView image; private TextView text; public IButton(Context context) { this(context, null); } public IButton(Context context, AttributeSet attrs) { this(context, attrs, 0); } public IButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.ibutton, this, true); layout = (RelativeLayout) view.findViewById(R.id.btn_layout); image = (ImageView) view.findViewById(R.id.btn_icon); text = (TextView) view.findViewById(R.id.btn_text); if (attrs != null) { TypedArray attributes = context.obtainStyledAttributes(attrs,R.styleable.IButtonStyle); Drawable drawable = attributes.getDrawable(R.styleable.IButtonStyle_button_icon); if(drawable != null) { image.setImageDrawable(drawable); } String str = attributes.getString(R.styleable.IButtonStyle_button_text); text.setText(str); attributes.recycle(); } } @Override public void setOnClickListener(final OnClickListener l) { super.setOnClickListener(l); layout.setOnClickListener(l); } public void setDrawable(int resId) { image.setImageResource(resId); } 

}

布局ibutton.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/btn_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" > <ImageView android:id="@+id/btn_icon" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="2dp" android:layout_toLeftOf="@+id/btn_text" android:duplicateParentState="true" /> <TextView android:id="@+id/btn_text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_centerInParent="true" android:duplicateParentState="true" android:gravity="center_vertical" android:textColor="#000000" /> </RelativeLayout> 

为了使用这个自定义小部件:

 <com.test.android.widgets.IButton android:id="@+id/new" android:layout_width="fill_parent" android:layout_height="@dimen/button_height" ibutton:button_text="@string/btn_new" ibutton:button_icon="@drawable/ic_action_new" /> 

您必须为自定义属性 xmlns 提供名称空间 :ibutton =“http://schemas.android.com/apk/res/com.test.android.xxx”,其中com.test.android.xxx是根包应用程序。;

只要把它放在xmlns:android =“http://schemas.android.com/apk/res/android”。;

你最后需要的是attrs.xml中的自定义属性。

在attrs.xml中

 <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="IButtonStyle"> <attr name="button_text" /> <attr name="button_icon" format="integer" /> </declare-styleable> <attr name="button_text" /> </resources> 

为了更好的定位,如果您想避免使用RelativeLayout定位的潜在问题,可将自定义Button包装在LinearLayout中。

请享用!

有类似的问题,但想要中心绘制没有文字和没有包装布局。 解决scheme是创build自定义button,并添加一个可绘制的除左,右,上,下。 人们可以很容易地修改drawable的位置,并将其放在相对于文本的所需位置。

CenterDrawableButton.java

 public class CenterDrawableButton extends Button { private Drawable mDrawableCenter; public CenterDrawableButton(Context context) { super(context); init(context, null); } public CenterDrawableButton(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public CenterDrawableButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public CenterDrawableButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(context, attrs); } private void init(Context context, AttributeSet attrs){ //if (isInEditMode()) return; if(attrs!=null){ TypedArray a = context.getTheme().obtainStyledAttributes( attrs, R.styleable.CenterDrawableButton, 0, 0); try { setCenterDrawable(a.getDrawable(R.styleable.CenterDrawableButton_drawableCenter)); } finally { a.recycle(); } } } public void setCenterDrawable(int center) { if(center==0){ setCenterDrawable(null); }else setCenterDrawable(getContext().getResources().getDrawable(center)); } public void setCenterDrawable(@Nullable Drawable center) { int[] state; state = getDrawableState(); if (center != null) { center.setState(state); center.setBounds(0, 0, center.getIntrinsicWidth(), center.getIntrinsicHeight()); center.setCallback(this); } mDrawableCenter = center; invalidate(); requestLayout(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if(mDrawableCenter!=null) { setMeasuredDimension(Math.max(getMeasuredWidth(), mDrawableCenter.getIntrinsicWidth()), Math.max(getMeasuredHeight(), mDrawableCenter.getIntrinsicHeight())); } } @Override protected void drawableStateChanged() { super.drawableStateChanged(); if (mDrawableCenter != null) { int[] state = getDrawableState(); mDrawableCenter.setState(state); mDrawableCenter.setBounds(0, 0, mDrawableCenter.getIntrinsicWidth(), mDrawableCenter.getIntrinsicHeight()); } invalidate(); } @Override protected void onDraw(@NonNull Canvas canvas) { super.onDraw(canvas); if (mDrawableCenter != null) { Rect rect = mDrawableCenter.getBounds(); canvas.save(); canvas.translate(getWidth() / 2 - rect.right / 2, getHeight() / 2 - rect.bottom / 2); mDrawableCenter.draw(canvas); canvas.restore(); } } } 

attrs.xml

 <resources> <attr name="drawableCenter" format="reference"/> <declare-styleable name="CenterDrawableButton"> <attr name="drawableCenter"/> </declare-styleable> </resources> 

用法

 <com.virtoos.android.view.custom.CenterDrawableButton android:id="@id/centerDrawableButton" android:layout_width="wrap_content" android:layout_height="wrap_content" app:drawableCenter="@android:drawable/ic_menu_info_details"/> 
 <style name="captionOnly"> <item name="android:background">@null</item> <item name="android:clickable">false</item> <item name="android:focusable">false</item> <item name="android:minHeight">0dp</item> <item name="android:minWidth">0dp</item> </style> <FrameLayout style="?android:attr/buttonStyle" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button style="@style/captionOnly" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:drawableLeft="@android:drawable/ic_delete" android:gravity="center" android:text="Button Challenge" /> </FrameLayout> 

将FrameLayout放置在LinearLayout中,并将方向设置为Horizo​​ntal。

你可以把button放在LinearLayout上

 <RelativeLayout android:layout_width="match_parent" android:layout_height="@dimen/activity_login_fb_height" android:background="@mipmap/bg_btn_fb"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/lblLoginFb" android:textColor="@color/white" android:drawableLeft="@mipmap/icon_fb" android:textSize="@dimen/activity_login_fb_textSize" android:text="Login with Facebook" android:gravity="center" /> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/btnLoginFb" android:background="@color/transparent" /> </RelativeLayout> 

我解决这个问题的方法是用轻量级的<View ../>元素来dynamicresize。 以下是几个可以实现的例子:

由Dev-iL演示

请注意,实例3中button的可点击区域与2中(即带有空格)相同,不同于实例4,其间没有“无法点击”的间隙。

码:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_marginStart="5dp" android:layout_marginLeft="5dp" android:layout_marginTop="10dp" android:layout_marginBottom="5dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:textStyle="bold" android:text="Example 1: Button with a background color:"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:textColor="@android:color/white" android:drawableLeft="@android:drawable/ic_secure" android:drawableStart="@android:drawable/ic_secure" android:background="@android:color/darker_gray" android:paddingStart="20dp" android:paddingEnd="20dp" android:layout_weight="0.3"/> <!-- Play around with the above 3 values to modify the clickable area and image alignment with respect to text --> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout> <TextView android:layout_marginStart="5dp" android:layout_marginLeft="5dp" android:layout_marginTop="20dp" android:layout_marginBottom="5dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:textStyle="bold" android:text="Example 2: Button group + transparent layout + spacers:"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:textColor="@android:color/white" android:drawableLeft="@android:drawable/ic_secure" android:drawableStart="@android:drawable/ic_secure" android:background="@android:color/darker_gray" android:paddingStart="10dp" android:paddingEnd="10dp" android:layout_weight="1"/> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:textColor="@android:color/white" android:drawableLeft="@android:drawable/ic_secure" android:drawableStart="@android:drawable/ic_secure" android:background="@android:color/darker_gray" android:paddingStart="10dp" android:paddingEnd="10dp" android:layout_weight="1"/> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:textColor="@android:color/white" android:drawableLeft="@android:drawable/ic_secure" android:drawableStart="@android:drawable/ic_secure" android:background="@android:color/darker_gray" android:paddingStart="10dp" android:paddingEnd="10dp" android:layout_weight="1"/> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout> <TextView android:layout_marginStart="5dp" android:layout_marginLeft="5dp" android:layout_marginTop="20dp" android:layout_marginBottom="5dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:textStyle="bold" android:text="Example 3: Button group + colored layout:"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/darker_gray"> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button style="?android:attr/buttonBarButtonStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:textColor="@android:color/white" android:drawableLeft="@android:drawable/ic_secure" android:drawableStart="@android:drawable/ic_secure" android:background="@android:color/darker_gray" android:paddingStart="10dp" android:paddingEnd="10dp" android:layout_weight="1"/> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button style="?android:attr/buttonBarButtonStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:textColor="@android:color/white" android:drawableLeft="@android:drawable/ic_secure" android:drawableStart="@android:drawable/ic_secure" android:background="@android:color/darker_gray" android:paddingStart="10dp" android:paddingEnd="10dp" android:layout_weight="1"/> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button style="?android:attr/buttonBarButtonStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:textColor="@android:color/white" android:drawableLeft="@android:drawable/ic_secure" android:drawableStart="@android:drawable/ic_secure" android:background="@android:color/darker_gray" android:paddingStart="10dp" android:paddingEnd="10dp" android:layout_weight="1"/> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout> <TextView android:layout_marginStart="5dp" android:layout_marginLeft="5dp" android:layout_marginTop="20dp" android:layout_marginBottom="5dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:textStyle="bold" android:text="Example 4 (reference): Button group + no spacers:"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/darker_gray"> <Button style="?android:attr/buttonBarButtonStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:textColor="@android:color/white" android:drawableLeft="@android:drawable/ic_secure" android:drawableStart="@android:drawable/ic_secure" android:background="@android:color/darker_gray" android:paddingStart="10dp" android:paddingEnd="10dp" android:layout_weight="1"/> <Button style="?android:attr/buttonBarButtonStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:textColor="@android:color/white" android:drawableLeft="@android:drawable/ic_secure" android:drawableStart="@android:drawable/ic_secure" android:background="@android:color/darker_gray" android:paddingStart="10dp" android:paddingEnd="10dp" android:layout_weight="1"/> <Button style="?android:attr/buttonBarButtonStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:textColor="@android:color/white" android:drawableLeft="@android:drawable/ic_secure" android:drawableStart="@android:drawable/ic_secure" android:background="@android:color/darker_gray" android:paddingStart="10dp" android:paddingEnd="10dp" android:layout_weight="1"/> </LinearLayout> </LinearLayout> 

如果你尝试使用android:gravity =“center_horizo​​ntal”,会发生什么?

我认为android:gravity =“center”应该可以工作

正如Rodja所build议的,在4.0之前,没有一种直接的方式来将文本与可绘图集中在一起。 而确实设置一个padding_left值确实将drawable从边界移开。 因此,我的build议是,在运行时,您可以计算出您的drawable所需要的左边界的多less个像素,然后使用setPadding将其传递给您。您的计算可能类似于

 int paddingLeft = (button.getWidth() - drawableWidth - textWidth) / 2; 

绘图的宽度是固定的,您可以查看它,也可以计算或猜测文本的宽度。

最后,您需要通过屏幕密度来填充多个填充值,您可以使用DisplayMetrics进行填充

我不testing它,但似乎你可以使用CenteredContentButton库

公共类DrawableCenterTextView扩展TextView {

 public DrawableCenterTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public DrawableCenterTextView(Context context, AttributeSet attrs) { super(context, attrs); } public DrawableCenterTextView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { Drawable[] drawables = getCompoundDrawables(); if (drawables != null) { Drawable drawableLeft = drawables[0]; Drawable drawableRight = drawables[2]; if (drawableLeft != null || drawableRight != null) { float textWidth = getPaint().measureText(getText().toString()); int drawablePadding = getCompoundDrawablePadding(); int drawableWidth = 0; if (drawableLeft != null) drawableWidth = drawableLeft.getIntrinsicWidth(); else if (drawableRight != null) { drawableWidth = drawableRight.getIntrinsicWidth(); } float bodyWidth = textWidth + drawableWidth + drawablePadding; canvas.translate((getWidth() - bodyWidth) / 2, 0); } } super.onDraw(canvas); } 

}

肮脏的修复。

 android:paddingTop="10dp" android:drawableTop="@drawable/ic_src" 

塑造正确的填充解决了目的。 但是,对于不同的屏幕,可以使用不同的填充,并将其放置在相应的值文件夹中。

使用RelativeLayout (容器)和android:layout_centerHorizo​​ntal =“true” ,我的示例:

  <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <CheckBox android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:layout_gravity="center" android:layout_marginBottom="5dp" android:layout_marginLeft="10dp" android:layout_marginTop="5dp" android:button="@drawable/bg_fav_detail" android:drawablePadding="5dp" android:text=" Favorite" /> </RelativeLayout> 

其他可能性保持Button主题。

 <Button android:id="@+id/pf_bt_edit" android:layout_height="@dimen/standard_height" android:layout_width="match_parent" /> <LinearLayout android:layout_width="0dp" android:layout_height="0dp" android:layout_alignBottom="@id/pf_bt_edit" android:layout_alignLeft="@id/pf_bt_edit" android:layout_alignRight="@id/pf_bt_edit" android:layout_alignTop="@id/pf_bt_edit" android:layout_margin="@dimen/margin_10" android:clickable="false" android:elevation="20dp" android:gravity="center" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="match_parent" android:adjustViewBounds="true" android:clickable="false" android:src="@drawable/ic_edit_white_48dp"/> <TextView android:id="@+id/pf_tv_edit" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="@dimen/margin_5" android:clickable="false" android:gravity="center" android:text="@string/pf_bt_edit"/> </LinearLayout> 

有了这个解决scheme,如果你的活动主题添加了@ color / your_color @ color / your_highlight_color,那么你可以在棒棒糖上涂上阴影和涟漪的Matherial主题,以及以前版本的平面button与你的颜色和高亮coor当你按下它。

而且,用这个解决scheme自动化图片

结果:首先在棒棒糖设备上第二:在棒棒糖设备上3:棒棒糖设备按下button

在这里输入图像说明

我使用paddingLefttextView与图标居中,并将textViewalignment到left | centerVertical 。 并为查看和图标之间的一个小的差距我用drawablePadding

  <Button android:id="@+id/have_it_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/textbox" android:drawableLeft="@drawable/have_it" android:drawablePadding="30dp" android:gravity="left|center_vertical" android:paddingLeft="60dp" android:text="Owner Contact" android:textColor="@android:color/white" /> 

这可能是一个旧的/closures的线程,但我到处search都没有find有用的东西,直到我决定创build自己的解决scheme。 如果任何人在这里试图寻找答案尝试这一个,可能会节省你一分钟的思考

  <LinearLayout android:id="@+id/llContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/selector" android:clickable="true" android:gravity="center" android:orientation="vertical" android:padding="10dp" android:textStyle="bold"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="This is a text" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon_image /> </LinearLayout> 

由于线性布局作为容器和具有select器的人,当你点击整个线性布局时,它看起来应该是这样。 如果你想要它是中心,使用相对的后端。 如果你想水平居中改变方向为水平。

请注意不要忘记将android:clickable =“true”添加到您的主容器(相对或线性)以执行操作。

这又可能是旧线程,但仍然可以帮助那里的人。

– 希望它有助于 – 开心。

这是我的解决scheme..

 <FrameLayout android:id="@+id/login_button_login" android:background="@drawable/apptheme_btn_default_holo_dark" android:layout_width="280dp" android:layout_gravity="center" android:layout_height="40dp"> <Button android:clickable="false" android:drawablePadding="15dp" android:layout_gravity="center" style="@style/WhiteText.Small.Bold" android:drawableLeft="@drawable/lock" android:background="@color/transparent" android:text="LOGIN" /> </FrameLayout> 

使用这个android:background="@drawable/ic_play_arrow_black_24dp"

只需将背景设置为您要居中的图标