相关疑难解决方法(0)

以编程方式设置android形状颜色

我正在编辑以使问题更简单,希望这有助于获得准确的答案.

说我有以下oval形状:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:angle="270"
           android:color="#FFFF0000"/>
    <stroke android:width="3dp"
            android:color="#FFAA0055"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

如何在活动类中以编程方式设置颜色?

android shape android-layout

149
推荐指数
9
解决办法
11万
查看次数

Lollipop的backgroundTint对Button没有影响

我的活动中有一个Button,我希望它有我主题的强调色.而不是像我们必须做的那样制作我自己的可绘制的Lollipop,我自然会想要使用新backgroundTint属性.

<Button
    android:id="@+id/btnAddCode"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:backgroundTint="@color/accent"
    android:text="@string/addressInfo_edit_addCode" />
Run Code Online (Sandbox Code Playgroud)

不幸的是它没有效果,按钮保持灰色.

我尝试了不同的值backgroundTintMode,但没有改变任何东西.

我也尝试在我的Activity中以编程方式进行,它没有改变任何东西.

addCodeView.findViewById(R.id.btnAddCode).setBackgroundTintList(
     getResources().getColorStateList(R.color.accent));
Run Code Online (Sandbox Code Playgroud)

为什么我的色彩被忽略了?

编辑:只是为了澄清,我确实在Lollipop设备上进行测试.其他小部件(例如EditText)正确并自动着色.

android tint android-layout

79
推荐指数
8
解决办法
7万
查看次数

如何以编程方式创建android形状背景?

如何以编程方式创建此形状?

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle">

    <solid android:color="#e67e22"/> 
    <corners
        android:topLeftRadius="0dp"
        android:topRightRadius="0dp"
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

我尝试过这个简单的功能,可以获得角落,颜色和设置形状:

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.category_header);

    GradientDrawable drawable = (GradientDrawable) linearLayout.getDrawable();

    float[] values = { 0.2f, 0.2f, 0.2f, 0.2f };
    drawable.setCornerRadii(values);
Run Code Online (Sandbox Code Playgroud)

但我得到了这个错误:

对于LinearLayout类型,方法getDrawable()未定义

android android-layout

44
推荐指数
5
解决办法
4万
查看次数

如何在Android中以编程方式更改形状的纯色?

我试图改变solid的颜色shape在我的Fragment

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/icon_circle_background">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            ...>
            <solid android:color="@color/md_cyan_400"/>
        </shape>
    </item>
    <item
        ...
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

我似乎无法从我的代码中访问它,而且我已经尝试了以下有关堆栈溢出的答案:

以编程方式设置android形状颜色

如何动态改变形状颜色?

在用作背景的 Drawable xml 中在运行时更改形状纯色

这些都不起作用或已经过时了。

我尝试更改颜色的片段:

public class GameFragment extends Fragment {
    private Theme currentTheme;
    private Circle currentCircle;
    private int currentOrganisationId;
    private List<Card> circleCards;
    private List<ImageButton> circleButtons;
    private int viewHeight;
    private int viewWidth;
    private int marginCard;
    private int[] tableStarts;
    private RelativeLayout background;
    private ViewTreeObserver vto;

    public GameFragment() {
        circleCards = new ArrayList<>();
        circleButtons = new …
Run Code Online (Sandbox Code Playgroud)

android shape android-imagebutton

6
推荐指数
1
解决办法
1778
查看次数

如何将颜色值动态传递给xml

我有一个将绘制椭圆形的xml,代码如下:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#61118"/>
    <stroke android:width="1sp" android:color="#1B434D" />
</shape>
Run Code Online (Sandbox Code Playgroud)

现在我在这里android:color="#61118"我需要从java类传递值,这可能吗?

如果没有,有没有其他办法?

xml android colors

4
推荐指数
1
解决办法
2892
查看次数

按下按钮即可更改文字颜色

当用户触摸此按钮时,如何更改按钮中的彩色文本?

这是我的shape.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >


 <item android:state_pressed="true" >
<shape android:shape="rectangle">
  <gradient android:startColor="#ffcc33" 
    android:endColor="#ffcc33"
    android:angle="270" />
  <corners android:radius="4dp" />
  <stroke android:width="2px" android:color="#FFFFFF" />
</shape>
</item>

  <item android:state_focused="true" >
<shape android:shape="rectangle">
  <gradient android:startColor="#ffcc33" 
    android:endColor="#ffcc33"
    android:angle="270" />
  <corners android:radius="4dp" />
  <stroke android:width="2px" android:color="#FFFFFF" />
</shape>
</item>

  <item >
<shape android:shape="rectangle">
  <gradient android:startColor="#333333" 
    android:endColor="#333333"
    android:angle="270" />
  <corners android:radius="4dp" />
  <stroke android:width="2px" android:color="#FFFFFF" />
</shape>
</item>

</selector>
Run Code Online (Sandbox Code Playgroud)

xml android colors android-layout

2
推荐指数
1
解决办法
1万
查看次数

在android中以编程方式更改形状颜色

我试图以编程方式更改形状的颜色,但它不起作用.

这是形状

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#9F2200"/>
    <stroke android:width="2dp" android:color="#fff" />
</shape>
Run Code Online (Sandbox Code Playgroud)

这是我如何使用它作为按钮的背景

<Button                
  android:id="@+id/ibtn_EA_ColorPick_new"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:layout_marginLeft="10dp"
  android:layout_marginRight="10dp"
  android:background="@drawable/round_button"
  />
Run Code Online (Sandbox Code Playgroud)

这就是我如何改变它的颜色

GradientDrawable bgShape = (GradientDrawable)btn_ColorPick.getBackground();
bgShape.setColor(Color.RED);
Run Code Online (Sandbox Code Playgroud)

但是,当我改变背景颜色时,它会从屏幕上删除按钮.

android android-button android-drawable android-shape

2
推荐指数
1
解决办法
1956
查看次数