相关疑难解决方法(0)

如何动态改变形状颜色?

我有

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
    <solid
       android:color="#FFFF00" />
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
</shape>

<TextView
    android:background="@drawable/test"
    android:layout_height="45dp"
    android:layout_width="100dp"
    android:text="Moderate"
/>
Run Code Online (Sandbox Code Playgroud)

所以现在我希望这个形状根据我从Web服务调用中获取的信息来改变颜色.所以它可能是黄色或绿色或红色或其他任何取决于我从网络电话呼叫收到的颜色.

如何更改形状的颜色?根据这些信息?

android shape android-layout xamarin.android

135
推荐指数
7
解决办法
12万
查看次数

如何以编程方式创建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万
查看次数

试图绘制一个按钮:如何设置笔触颜色以及如何在不知道高度的情况下将渐变"对齐"到底部?

我正在以编程方式创建一个按钮.它是圆形的,有渐变背景,工作正常,看起来不错,但我不能做我想要的两件事:

  1. 使用给定颜色设置1像素笔划.我尝试了getPaint().setStroke(),但无法弄清楚如何设置笔触颜色.我该怎么办?
  2. 将渐变与按钮底部对齐,无论其高度如何.这可能吗?

作为参考,这是我正在使用的代码:

Button btn = new Button(context);
btn.setPadding(7, 3, 7, 5);
btn.setTextColor(text_color);

// Create a gradient for the button. Height is hardcoded to 30 (I don't know the height beforehand). 
// I wish I could set the gradient aligned to the bottom of the button.
final Shader shader = new LinearGradient(0, 0, 0, 30,
    new int[] { color_1, color_2 },
    null, Shader.TileMode.MIRROR);

float[] roundedCorner = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 }
ShapeDrawable …
Run Code Online (Sandbox Code Playgroud)

android

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

如何在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
查看次数

如何以编程方式更改形状纯色?

我如何以<solid android:color= />编程方式更改?

我定义了一个自定义形状元素:my_item.xml:

<shape android:shape="oval">
    <solid android:color="#FFFF0000"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

并在另一个布局中重用它:grid_view.xml:

<LinearLayout>
    <ImageView ... android:src="@drawable/my_item"
                   android:id="@+id/myitemid" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

以下不起作用:

public class ShapeItemAdapter extends BaseAdapter {

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
          view = inflter.inflate(R.layout.grid_view, null);
          ImageView shape = (ImageView) view.findViewById(R.id.myitemid);
          shape.setBackgroundColor(0xBCCACA); //does not work
    }
}
Run Code Online (Sandbox Code Playgroud)

android

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

背景形状在Android的API级别16中无法完美运行

我设计了一种形状来应用于线性布局的背景。它在API级别21上可以完美运行,但在API级别16上却无法正常工作,请帮助我。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
    <stroke
       android:drawable="@android:id/background"
       android:width="3dp"
       android:color="#023e64">
    </stroke>

    <corners
        android:bottomLeftRadius="16dp"
        android:bottomRightRadius="16dp"
        android:topLeftRadius="16dp"
        android:topRightRadius="16dp"/>

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

xml android

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

以编程方式将颜色指定给形状

我有这样的布局:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/user_color">
        <shape android:shape="rectangle">
            <solid android:color="#000000" />
        </shape>
    </item>
    <item android:right="25dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/user_background" />
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

我怎么称呼形状:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/shape">
Run Code Online (Sandbox Code Playgroud)

如何以编程方式更改带有id的矩形形状的颜色user_color

android android-layout android-xml

0
推荐指数
1
解决办法
127
查看次数