【问题标题】:Android: How to make fix text of the button when add left drawableAndroid:添加左可绘制对象时如何修复按钮的文本
【发布时间】:2019-12-12 22:35:47
【问题描述】:

我创建了自定义视图。在我的课堂上,我创建了一个按钮:

button = new AppCompatButton(getContext());
LayoutParams button_params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
button_params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
button.setLayoutParams(button_params);
button.setText(TextUtils.isEmpty(progressText) ? "Button" : progressText);
button.setGravity(Gravity.CENTER);
button.setOnClickListener(v -> {
    progressBar.setVisibility(VISIBLE);
});
addView(button);

我向这个按钮添加了一个drawable,如下所示:

   if (isDone) {
        progressBar.setVisibility(GONE);
        Drawable drawable = getResources().getDrawable(progressIconSuccess);
        drawable.setBounds(0, 0, 100, 100);
        button.setCompoundDrawables(drawable, null, null, null);
    } 

但是当drawable添加到按钮时,按钮文本向右移动:

在添加可绘制对象之前:

添加drawable后:

我不想将按钮文本向右移动。我想保持固定而不改变它的位置。我该怎么做?

我以这种方式使用我的自定义视图:

<com.tazik.progressbutton.ProgressButton
    android:id="@+id/pb_button"
    android:layout_width="150dp"
    android:layout_height="70dp"
    app:progress_height="30"
    app:progress_width="30"
    app:progress_text = "Register"
    app:progress_iconfail="@drawable/ic_fail"
    app:progress_iconSuccess="@drawable/ic_done"/>

【问题讨论】:

    标签: android android-custom-view android-button


    【解决方案1】:

    使用新的MaterialIconButton

    按钮 XML 代码

     <com.google.android.material.button.MaterialButton
        android:id="@+id/btn"
        style="@style/Widget.MaterialComponents.Button.Icon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Icon Button"
         />
    

    活动代码:

    public class MainActivity extends AppCompatActivity {
    
        MaterialButton iconButton;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.testing);
    
            iconButton=findViewById(R.id.btn);
            iconButton.setIcon(R.drawable.your_icon);
            iconButton.setIconGravity(MaterialButton.ICON_GRAVITY_START);
        }
    }
    

    【讨论】:

    • 当我在课堂上创建AppCompatButton 时,如何添加setIconGravity?因为现在我在按钮实例中没有这个方法@sumitshukla
    • 不能,因为它是 MaterialIconButton 的属性。
    【解决方案2】:

    progressBar.setVisibility(GONE) 更改为progressBar.setVisibility(INVISIBLE)

    正如名称所暗示的,当您将可见性设置为 GONE 时,视图“物理上”消失了 - 它不存在,而 INVISIBLE 仍然存在,只是没有被绘制。

    【讨论】:

    • 我改成了progressBar.setVisibility(INVISIBLE),但按钮的文本仍然向右移动@miku
    • 在xml中是否也设置为不可见?可能是它已经消失了,所以当应用程序启动时它认为它已经消失了。
    • 我不能这样做,因为我的自定义布局是从relativeLayoutand 扩展的,并且我方便地制作按钮和进度条,然后我将这些视图添加到父布局(RelativeLayout)。这是我的课stackoverflow.com/questions/57352639/…@miku
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    相关资源
    最近更新 更多