比较来自世界各地的卖家的域名和 IT 服务价格

如何将图标和文本居中 android button 宽度设置 "fill parent"

我希望有 Android Button 使用图标+文本以内部为中心。 我使用属性 drawableLeft 要安装图像,它会很好地运行 button 有宽度
"wrap_content"

, 但我需要伸展到最大宽度,所以我使用宽度
"fill_parent"

. 它将我的图标左转移动 button, 我想要这个图标,文本集中在里面 button.

我试图配置填充,但它允许您只提供固定值,因此这不是我需要的。 我需要+文本图标在中心对齐。


<button android:drawableleft="@drawable/start" android:heigh="wrap_content" android:id="@+id/startTelemoteButton" android:paddingleft="20dip" android:paddingright="20dip" android:text="@string/start_telemote" android:width="fill_parent"></button>


是否有关于我如何实现这一目标的建议?
已邀请:

知食

赞同来自:

android:drawableLeft 总是拥抱 android:paddingLeft 距离左边边界一段距离。 什么时候 button 未安装 android:width="wrap_content", 他将永远挂在左边!

使用属性 Android 4.0 /等级 14 API/ 您可以使用该属性 android:drawableStart 在文本的开头上容纳绘图。 我想到的唯一向后兼容解决方案 - 这是使用
http://developer.android.com/r ... .html
要创建文本+覆盖的图像:


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/;


在我的情况下,我也需要配置属性 android:gravity Button, 所以它看起来居中:


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

知食

赞同来自:

我知道我迟到了这个问题的答案,但它帮助了我:


<framelayout android:background="@color/fb" android:layout_height="35dp" android:layout_marginbottom="5dp" android:layout_margintop="10dp" android:layout_width="match_parent">
<button android:background="@null" android:drawableleft="@drawable/ic_facebook" android:gravity="center" android:id="@+id/fbLogin" android:layout_gravity="center" android:layout_height="match_parent" android:layout_width="wrap_content" android:minheight="0dp" android:minwidth="0dp" android:text="FACEBOOK" android:textcolor="@android:color/white"></button>
</framelayout>


我从这里找到了这个解决方案:
http://porcupineprogrammer.blo ... .html

涵秋

赞同来自:

以前的所有答案都似乎已经过时了

现在你可以使用
MaterialButton

, 这允许您设置图标的重力。


<com.google.android.material.button.materialbutton android:gravity="center" android:id="@+id/btnDownloadPdf" android:layout_height="56dp" android:layout_margin="16dp" android:layout_width="0dp" android:textallcaps="true" app:backgroundtint="#fc0" app:icon="@drawable/ic_pdf" app:icongravity="textStart" app:iconpadding="10dp" app:icontint="#f00" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintend_toendof="parent" app:layout_constraintstart_tostartof="parent" tools:text="Download Pdf"></com.google.android.material.button.materialbutton>


https://i.stack.imgur.com/jX67S.png
要使用材料组件,您可能需要:

添加依赖项
implementation 'com.google.android.material:material:1.3.0-alpha01'

/使用最新版本/

使您的主题扩展了材料组件的主题


<style name="AppTheme" parent="Theme.MaterialComponents.Light">
...
</style>


如果您无法执行此操作,请从主题中扩展 Material Bridge


<style name="AppTheme" parent="Theme.MaterialComponents.Light.Bridge">
...
</style>

莫问

赞同来自:

我用了

LinearLayout

反而

Button

. OnClickListener

, 我需要使用的是完全工作 LinearLayout.


<linearlayout android:background="@drawable/selector" android:clickable="true" android:gravity="center" android:id="@+id/my_button" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal">
<imageview android:adjustviewbounds="true" android:layout_height="wrap_content" android:layout_marginright="15dp" android:layout_width="wrap_content" android:scaletype="fitCenter" android:src="@drawable/icon"></imageview>
<textview android:gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Text"></textview>
</linearlayout>

八刀丁二

赞同来自:

我通过向左和右侧添加缩进来设置它,如下所示:


<button android:background="@color/white" android:drawableleft="@drawable/like" android:drawablestart="@drawable/like" android:gravity="center" android:id="@+id/btn_facebookact_like" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="0dp" android:paddingleft="40dp" android:paddingright="40dp" android:text="@string/btn_facebookact_like" android:textallcaps="false" android:textcolor="@color/black"></button>

八刀丁二

赞同来自:

我最近面对同样的问题。 我试图找到更便宜的解决方案,所以我想出了它。


<linearlayout android:background="@drawable/selector_button_translucent_ab_color" android:clickable="true" android:descendantfocusability="blocksDescendants" android:gravity="center" android:id="@+id/linearButton" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="horizontal">
<imageview android:contentdescription="@string/app_name" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/ic_launcher"></imageview>
<textview android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/app_name" android:textcolor="@android:color/white"></textview>
</linearlayout>


然后打电话
OnClickListener


LinearLayout

.

我希望这将有助于某人,因为它似乎是一个非常普遍的问题。 :/

喜特乐

赞同来自:

您可以使用自定义 button, 哪些措施并吸引自己发布左侧绘图。 请找到一个例子和使用。
https://gist.github.com/rajivnarayana/5224881

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:drawableleft="@drawable/my_drawable" android:gravity="center" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="0dp" android:padding="7dp" android:text="My Text"></com.mypackage.drawablealignedbutton>

小明明

赞同来自:

这是我写的决定 3 几年前。 Button 有文本和左图标,并在一个实际的框架中 button 这里可以拉伸 fill_parent. 我再也找不到了,但他工作了。 大概, Button 无需使用,可以替换 TextView, 但我现在不会测试它,这里它不会改变太多功能。


<framelayout android:background="@drawable/apptheme_btn_default_holo_dark" android:id="@+id/login_button_login" android:layout_gravity="center" android:layout_height="40dp" android:layout_width="fill_parent">
<button android:background="@color/transparent" android:clickable="false" android:drawableleft="@drawable/lock" android:drawablepadding="15dp" android:layout_gravity="center" android:text="LOGIN" style="@style/WhiteText.Small.Bold"></button>
</framelayout>

喜特乐

赞同来自:

我知道这个问题有点旧,但也许你仍然为提示或旁路轨道打开:

创建 RelativeLayout "wrap_content" 用镜子 button 作为背景或你自己 button 作为布局的第一个元素。 拿 LinearLayout 并安装它 "layout_centerInParent" 和 "wrap_content". 然后安装你的 Drawable 作为 Imageview. 最后,安装 TextView 用你的文字 /locale/.

因此,主要是您拥有这种结构:


RelativeLayout
Button
LinearLayout
ImageView
TextView


或者喜欢这个:


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


我知道,通过这个解决方案,有许多您需要处理的物品,但您可以很轻松地创建自己的自定义 button 与它一起,并设置文本和图纸的确切位置 :/

帅驴

赞同来自:

我解决这个问题的方法是环绕 button 轻质元素
<view ..=""></view>

, 它动态地改变大小。 以下是其中可以实现的内容:

https://i.stack.imgur.com/vGIdF.png
请注意,示例中的按钮可镀块区域 3 与示例中的相同 2 /即用空间/, 什么不同的例子 4, 他们之间没有间隔 "unclickable".

代码:


xml version="1.0" encoding="utf-8"?
<linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="[url=http://schemas.android.com/apk/res/android">]http://schemas.android.com/apk ... gt%3B[/url]
<textview android:layout_height="wrap_content" android:layout_marginbottom="5dp" android:layout_marginleft="5dp" android:layout_marginstart="5dp" android:layout_margintop="10dp" android:layout_width="match_parent" android:text="Example 1: Button with a background color:" android:textstyle="bold"></textview>
<linearlayout android:layout_height="wrap_content" android:layout_width="match_parent">
<view android:layout_height="match_parent" android:layout_weight="1" android:layout_width="0dp"></view>
<button android:background="@android:color/darker_gray" android:drawableleft="@android:drawable/ic_secure" android:drawablestart="@android:drawable/ic_secure" android:layout_height="wrap_content" android:layout_weight="0.3" android:layout_width="wrap_content" android:paddingend="20dp" android:paddingstart="20dp" android:text="Button" android:textcolor="@android:color/white"></button>
<!-- Play around with the above 3 values to modify the clickable
area and image alignment with respect to text -->
<view android:layout_height="match_parent" android:layout_weight="1" android:layout_width="0dp"></view>
</linearlayout>
<textview android:layout_height="wrap_content" android:layout_marginbottom="5dp" android:layout_marginleft="5dp" android:layout_marginstart="5dp" android:layout_margintop="20dp" android:layout_width="match_parent" android:text="Example 2: Button group + transparent layout + spacers:" android:textstyle="bold"></textview>
<linearlayout android:layout_height="wrap_content" android:layout_width="match_parent">
<view android:layout_height="match_parent" android:layout_weight="1" android:layout_width="0dp"></view>
<button android:background="@android:color/darker_gray" android:drawableleft="@android:drawable/ic_secure" android:drawablestart="@android:drawable/ic_secure" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:paddingend="10dp" android:paddingstart="10dp" android:text="Button" android:textcolor="@android:color/white"></button>
<view android:layout_height="match_parent" android:layout_weight="1" android:layout_width="0dp"></view>
<button android:background="@android:color/darker_gray" android:drawableleft="@android:drawable/ic_secure" android:drawablestart="@android:drawable/ic_secure" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:paddingend="10dp" android:paddingstart="10dp" android:text="Button" android:textcolor="@android:color/white"></button>
<view android:layout_height="match_parent" android:layout_weight="1" android:layout_width="0dp"></view>
<button android:background="@android:color/darker_gray" android:drawableleft="@android:drawable/ic_secure" android:drawablestart="@android:drawable/ic_secure" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:paddingend="10dp" android:paddingstart="10dp" android:text="Button" android:textcolor="@android:color/white"></button>
<view android:layout_height="match_parent" android:layout_weight="1" android:layout_width="0dp"></view>
</linearlayout>
<textview android:layout_height="wrap_content" android:layout_marginbottom="5dp" android:layout_marginleft="5dp" android:layout_marginstart="5dp" android:layout_margintop="20dp" android:layout_width="match_parent" android:text="Example 3: Button group + colored layout:" android:textstyle="bold"></textview>
<linearlayout android:background="@android:color/darker_gray" android:layout_height="wrap_content" android:layout_width="match_parent">
<view android:layout_height="match_parent" android:layout_weight="1" android:layout_width="0dp"></view>
<button android:background="@android:color/darker_gray" android:drawableleft="@android:drawable/ic_secure" android:drawablestart="@android:drawable/ic_secure" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:paddingend="10dp" android:paddingstart="10dp" android:text="Button" android:textcolor="@android:color/white" style="?android:attr/buttonBarButtonStyle"></button>
<view android:layout_height="match_parent" android:layout_weight="1" android:layout_width="0dp"></view>
<button android:background="@android:color/darker_gray" android:drawableleft="@android:drawable/ic_secure" android:drawablestart="@android:drawable/ic_secure" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:paddingend="10dp" android:paddingstart="10dp" android:text="Button" android:textcolor="@android:color/white" style="?android:attr/buttonBarButtonStyle"></button>
<view android:layout_height="match_parent" android:layout_weight="1" android:layout_width="0dp"></view>
<button android:background="@android:color/darker_gray" android:drawableleft="@android:drawable/ic_secure" android:drawablestart="@android:drawable/ic_secure" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:paddingend="10dp" android:paddingstart="10dp" android:text="Button" android:textcolor="@android:color/white" style="?android:attr/buttonBarButtonStyle"></button>
<view android:layout_height="match_parent" android:layout_weight="1" android:layout_width="0dp"></view>
</linearlayout>
<textview android:layout_height="wrap_content" android:layout_marginbottom="5dp" android:layout_marginleft="5dp" android:layout_marginstart="5dp" android:layout_margintop="20dp" android:layout_width="match_parent" android:text="Example 4 /reference/: Button group + no spacers:" android:textstyle="bold"></textview>
<linearlayout android:background="@android:color/darker_gray" android:layout_height="wrap_content" android:layout_width="match_parent">
<button android:background="@android:color/darker_gray" android:drawableleft="@android:drawable/ic_secure" android:drawablestart="@android:drawable/ic_secure" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:paddingend="10dp" android:paddingstart="10dp" android:text="Button" android:textcolor="@android:color/white" style="?android:attr/buttonBarButtonStyle"></button>
<button android:background="@android:color/darker_gray" android:drawableleft="@android:drawable/ic_secure" android:drawablestart="@android:drawable/ic_secure" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:paddingend="10dp" android:paddingstart="10dp" android:text="Button" android:textcolor="@android:color/white" style="?android:attr/buttonBarButtonStyle"></button>
<button android:background="@android:color/darker_gray" android:drawableleft="@android:drawable/ic_secure" android:drawablestart="@android:drawable/ic_secure" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:paddingend="10dp" android:paddingstart="10dp" android:text="Button" android:textcolor="@android:color/white" style="?android:attr/buttonBarButtonStyle"></button>
</linearlayout>
</linearlayout>

小姐请别说爱

赞同来自:

您可以创建自己的小部件:

班级 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 android:clickable="true" android:id="@+id/btn_layout" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="[url=http://schemas.android.com/apk/res/android">]http://schemas.android.com/apk ... gt%3B[/url]
<imageview android:duplicateparentstate="true" android:id="@+id/btn_icon" android:layout_height="fill_parent" android:layout_marginright="2dp" android:layout_toleftof="@+id/btn_text" android:layout_width="wrap_content"></imageview>
<textview android:duplicateparentstate="true" android:gravity="center_vertical" android:id="@+id/btn_text" android:layout_centerinparent="true" android:layout_height="fill_parent" android:layout_width="wrap_content" android:textcolor="#000000"></textview>
</relativelayout>


要使用此自定义小部件

:


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


您必须为用户属性提供命名空间。

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.

在40年代


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


更好地定位包装用户 Button 里面 LinearLayout, 如果你想避免潜在的定位问题 RelativeLayout.

享受!

风见雨下

赞同来自:

我有一个类似的问题,但我想在没有文本的绘图中心,没有包裹的布局。 解决方案是创建自定义 button 并再添加一个 drawable 此外 LEFT, RIGHT, TOP, BOTTOM. 您可以轻松更改绘图住宿,并在相对于文本中排列在所需位置。

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 format="reference" name="drawableCenter"></attr>
<declare-styleable name="CenterDrawableButton">
<attr name="drawableCenter"></attr>
</declare-styleable>
</resources>


使用


<com.virtoos.android.view.custom.centerdrawablebutton android:id="@id/centerDrawableButton" android:layout_height="wrap_content" android:layout_width="wrap_content" app:drawablecenter="@android:drawable/ic_menu_info_details"></com.virtoos.android.view.custom.centerdrawablebutton>

诸葛浮云

赞同来自:

<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 android:layout_height="wrap_content" android:layout_width="match_parent" style="?android:attr/buttonStyle">
<button android:drawableleft="@android:drawable/ic_delete" android:gravity="center" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button Challenge" style="@style/captionOnly"></button>
</framelayout>


放 FrameLayout 在 LinearLayout 并水平设置方向。

诸葛浮云

赞同来自:

你可以放置 button 地面 LinearLayout


<relativelayout android:background="@mipmap/bg_btn_fb" android:layout_height="@dimen/activity_login_fb_height" android:layout_width="match_parent">
<linearlayout android:gravity="center" android:layout_height="match_parent" android:layout_width="match_parent">
<textview android:drawableleft="@mipmap/icon_fb" android:gravity="center" android:id="@+id/lblLoginFb" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Login with Facebook" android:textcolor="@color/white" android:textsize="@dimen/activity_login_fb_textSize"></textview>
</linearlayout>
<button android:background="@color/transparent" android:id="@+id/btnLoginFb" android:layout_height="match_parent" android:layout_width="match_parent"></button>
</relativelayout>

江南孤鹜

赞同来自:

如果你试试,会发生什么 android:gravity="center_horizontal"?

卫东

赞同来自:

你是怎么认为的 4.0 没有直接的方式来居中文本 drawable. 实际上,设定值 padding_left 真的从边框移动一个绘制的对象。 因此,我的提议是在执行期间,您可以完全计算左侧边框的像素应该从绘图中进行多少,然后通过它使用
http://developer.android.com/r ... 28int,%20int,%20int,%20int%29
.


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


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

最后,您需要乘以填充屏幕密度的价值
http://developer.android.com/r ... .html

八刀丁二

赞同来自:

我觉得 android:gravity= "centre" 应该管用

君笑尘

赞同来自:

我不测试它,但似乎你可以使用
https://github.com/steelkiwi/CenteredContentButton
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_centerHorizontal="true"

, 我的样本:


<relativelayout android:layout_height="match_parent" android:layout_weight="1" android:layout_width="0dp">
<checkbox android:button="@drawable/bg_fav_detail" android:drawablepadding="5dp" android:layout_centerhorizontal="true" android:layout_gravity="center" android:layout_height="match_parent" android:layout_marginbottom="5dp" android:layout_marginleft="10dp" android:layout_margintop="5dp" android:layout_width="wrap_content" android:text=" Favorite"></checkbox>
</relativelayout>

知食

赞同来自:

另一个拯救主题的机会 Button.


<button android:id="@+id/pf_bt_edit" android:layout_height="@dimen/standard_height" android:layout_width="match_parent"></button>
<linearlayout android:clickable="false" android:elevation="20dp" android:gravity="center" 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_height="0dp" android:layout_margin="@dimen/margin_10" android:layout_width="0dp" android:orientation="horizontal">
<imageview android:adjustviewbounds="true" android:clickable="false" android:layout_height="match_parent" android:layout_width="wrap_content" android:src="@drawable/ic_edit_white_48dp"></imageview>
<textview android:clickable="false" android:gravity="center" android:id="@+id/pf_tv_edit" android:layout_height="match_parent" android:layout_marginleft="@dimen/margin_5" android:layout_width="wrap_content" android:text="@string/pf_bt_edit"></textview>
</linearlayout>


使用此解决方案,如果您添加
@color/your_color
@color/your_highlight_color
在您的活动主题中,您可以在带阴影和涟漪的棒棒糖上拥有一个重要的主题,并为以前的版本 flat button 用你的颜色和照明 coor 当你点击它时。

此外,在该决定的帮助下,图片是自动化的

结果 :
首先在棒棒糖设备上
其次 : 在棒棒糖设备上
3-y。 : 棒棒糖设备按压 button

https://i.stack.imgur.com/eufDz.png

诸葛浮云

赞同来自:

我以中心为中心
textView

使用图标
paddingLeft

和平整 textView

左边| deltervertical.

, 并且在视图和使用的图标之间进行小区间隔
drawablePadding



<button android:background="@drawable/textbox" android:drawableleft="@drawable/have_it" android:drawablepadding="30dp" android:gravity="left|center_vertical" android:id="@+id/have_it_main" android:layout_height="wrap_content" android:layout_width="match_parent" android:paddingleft="60dp" android:text="Owner Contact" android:textcolor="@android:color/white"></button>

帅驴

赞同来自:

它可能是旧的/一个封闭的话题,但我正在寻找到处并没有找到任何有用的东西,直到我决定创造自己的决定。 如果有人试图在这里找到一个答案,请尝试这样,也许他会为您节省一分钟的反思


<linearlayout android:background="@drawable/selector" android:clickable="true" android:gravity="center" android:id="@+id/llContainer" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" android:padding="10dp" android:textstyle="bold">
<textview android:gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="This is a text"></textview>
<imageview ,="" -="" <="" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/icon_image /&gt;


&lt;/LinearLayout&gt;


由于线性布局充当容器,并且在点击整个线性布局时具有选择器的人,因此它将完全相同。 如果您希望它同时居中,请使用相对 insteaf. 如果您希望水平居中,请更改水平的方向。

笔记,

那个ne。

忘记添加

android:clickable=" div="" true"="" 再次,它可能是一个旧的线程,但仍然可以帮助那里的人。="" 在你的主容器中="" 快乐的编码。="" 相对或线性="" 赫雷,我希望它会有所帮助="" 这样行动发生了。="">
</imageview></linearlayout>

要回复问题请先登录注册