【问题标题】:Setting % of a background color programmatically以编程方式设置背景颜色的百分比
【发布时间】:2015-06-09 19:00:04
【问题描述】:

假设我只想为线性布局的背景着色 64%。我想从左到右给它上色,让它成为纯色。

起初我尝试使用 Gradient Drawables,因为它允许我指定颜色的方向和颜色……但它不允许我指定百分比。如果我可以在 xml drawable 中做到这一点,那将是理想的,但百分比会发生变化,我需要根据百分比重新调整背景。

这是我为整个视图着色的代码。有没有办法只为视图的 35% 而不是整个视图着色?

GradientDrawable gradient = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
                                        new int[] {getContext().getResources().getColor(R.color.icitizen_poll_opaque_gold),
                                        getContext().getResources().getColor(R.color.icitizen_poll_opaque_gold)});
                                gradient.setCornerRadius(1f);
                                v.setBackground(gradient);

【问题讨论】:

  • 你已经很接近了:使用 ClipDrawable 代替 GradientDrawable,忘记下面的答案以及其他视图等......

标签: android view gradient background-color


【解决方案1】:

我认为这是不可能的,您可以尝试使用嵌套的线性布局并使用权重属性(指定百分比)

示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.7"
        android:background="@android:color/darker_gray">

     </LinearLayout>

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3"
        android:background="@android:color/black">

    </LinearLayout>




</LinearLayout>

【讨论】:

    【解决方案2】:

    完全有可能,方法很少,但我建议使用自定义 Drawable,例如:

    public class PercentDrawable extends Drawable {
    
    private final int percent;
    private final Paint paint;
    
    public PercentDrawable(int percent) {
        super();
    
        this.percent = percent;
        this.paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        this.paint.setColor(0xffff0000);
    }
    
    @Override
    public void draw(Canvas canvas) {
        canvas.drawRect(0, 0, canvas.getWidth(), percent * canvas.getHeight() / 100, paint);
    }
    
    @Override
    public void setAlpha(int i) {
    
    }
    
    @Override
    public void setColorFilter(ColorFilter colorFilter) {
    
    }
    
    @Override
    public int getOpacity() {
        return 0;
    }
    

    }

    ...并使用它:

    public class MainActivity extends ActionBarActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        findViewById(R.id.background).setBackground(new PercentDrawable(30));
    } //... rest of Activity
    

    R.id.background 是带有 match_parent w/h 的简单 LinearLayout,也可以是简单的 View。 xml 布局应该尽量简单。

    【讨论】:

    • 正是我想要的。谢谢楼主!
    • @cj1098 自定义 Drawable 是一个不错的尝试,但 ClipDrawable 只需 2 或 3 行代码就可以做到这一点
    【解决方案3】:

    您可以使用FrameLayout 在您的下创建另一个布局并为其背景着色。

    <FrameLayout
       android:layout_height="match_parent"
       android:layout_width="match_parent">
       <LinearLayout
         android:layout_height="match_parent"
         android:layout_width="0dp"/> // set 35% of screen programmatically
       <LinearLayout
         android:layout_height="match_parent"
         android:layout_width="match_parent"/>
    </FrameLayout>
    

    How to get screen size:

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
    

    【讨论】:

    • 但是我如何为它的百分比着色?这就是我遇到的问题。
    • 以编程方式获取屏幕尺寸并将其设置为您的视图
    猜你喜欢
    • 2021-10-18
    • 1970-01-01
    • 2014-06-24
    • 2021-05-04
    • 2023-03-26
    • 2011-01-09
    • 2019-04-22
    • 1970-01-01
    • 2016-12-12
    相关资源
    最近更新 更多