代码之家  ›  专栏  ›  技术社区  ›  Kristiyan Petrov

Android-如何在Android M上将按钮的文本颜色设置为文本

  •  2
  • Kristiyan Petrov  · 技术社区  · 8 年前

    在Android M上

    button.setText("✔");
    button.setTextColor(Color.WHITE);
    

    不工作有什么原因吗?

    我怎样才能得到白色的?

    3 回复  |  直到 8 年前
        1
  •  1
  •   Amit Vaghela    8 年前

    试着这样做,

    在xml文件中,

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@mipmap/ic_launcher"
            android:hint="hii" />
    </LinearLayout >
    

    现在,在java文件中添加如下代码

    public class SixthActivity extends AppCompatActivity {
    
        Button btn;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.first_activity);
            btn = (Button) findViewById(R.id.btn);
    
            btn.setText("");
            btn.setBackgroundColor(getResources().getColor(R.color.white));
            btn.setBackgroundResource(R.mipmap.ic_launcher);
        }
    }
    
        2
  •  0
  •   Aditya Vyas-Lakhan user1796260    8 年前

    您也可以使用unicode作为刻度线,请尝试使用 &#x2713; 并将其设置为文本视图。

        3
  •  0
  •   Community T.Woody    7 年前

    实际上,您可以添加可绘制的视图。类似于xml:

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load More"
        android:id="@+id/loadMore"
        android:drawableLeft="@drawable/your_marked_icon"
        android:drawableRight="@drawable/another_icon_if_need"
        style="?android:attr/borderlessButtonStyle"
        android:layout_centerHorizontal="true" />
    

    也可以在代码中这样做:

    Drawable top = getResources().getDrawable(R.drawable.image);
    //placement is like that : left, top, right, bottom
    button.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
    

    我将此解决方案用于: How to set property “android:drawableTop” of a button at runtime

    此外,您还可以更改可绘制的色调值,如下所示:

    public static Drawable setTint(Drawable d, int color) {
        Drawable wrappedDrawable = DrawableCompat.wrap(d);
        DrawableCompat.setTint(wrappedDrawable, color);
        return wrappedDrawable;
    }
    

    此解决方案基于: change color of drawable