【问题标题】:setting button border which already has a background color设置已经有背景颜色的按钮边框
【发布时间】:2012-04-10 17:01:21
【问题描述】:

我是 mono for android 的绝对初学者。

以下按钮是动态创建的并分配了一些背景颜色。

如何为每个按钮分配具有特定厚度的黑色边框?(请参阅下面的屏幕截图)。 左图是现在的样子,右图是应该的样子。

我在 SO 上推荐了 thisthis,但他们没有提供我需要的指导。

任何帮助表示赞赏...

编辑

按钮背景设置代码:

int[] colors=GetColorForScrips(decimal.Parse(_result.Rows[i]["Change(%)"].ToString ()));
btn.SetBackgroundColor(Color.Rgb(colors[0],colors[1],colors[2]));

GetColorForScrips() 中,我根据返回的 RGB 分量传递一个浮点值。

注意:

  1. 我使用 Mono for Android 作为我的 IDE,而不是 Eclipse

  2. 我正在使用上述代码 sn-p 分配背景颜色。

  3. 如果我使用btn.SetBackgroundDrawable(Resource.Drawable.Heatmap_Border);,它会给我can't convert from int to drawable 的错误。

  4. 如果我使用btn.SetBackgroundResource(Resource.Drawable.Heatmap_Border);,它会给我全黑屏,即按钮是可点击的,但不可见

建议的输出:

如上右图所示,每个按钮都会有一个特定的背景,具体取决于某个值。此背景是动态设置的。

我还想在按钮上使用黑色边框。

但我想这里最大的问题是我不能使用来自 btn.SetBackgroundDrawable() OR btn.SetBackgroundResource() 的任何两个或 btn.SetBackgroundColor() 在一起。

在这种情况下,只有稍后才会实施。

有什么办法???

最终编辑

正如其中一位用户所建议的那样,这非常有效......(GetColorForScrips() 基于浮点值返回 RGB 值)。

GradientDrawable drawable = new GradientDrawable();
drawable.SetShape(ShapeType.Rectangle);
drawable.SetStroke(1, Color.Black);
                
int[] colors=GetColorForScrips(decimal.Parse(result.Rows[i]["Change(%)"].ToString ()));
drawable.SetColor(Color.Rgb(colors[0],colors[1],colors[2]))             
btn.SetBackgroundDrawable(drawable);

【问题讨论】:

    标签: c# android button border xamarin.android


    【解决方案1】:

    使用此代码

    在drawable文件夹中创建一个button_bg.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
         android:shape="rectangle" android:padding="10dp">
      <solid android:color="@color/background_color"/>
      <corners
         android:bottomRightRadius="5dp"
         android:bottomLeftRadius="5dp"
         android:topLeftRadius="5dp"
         android:topRightRadius="5dp"/>
      <stroke android:width="2dp" android:color="@color/borderline_color" />
    </shape>
    

    而不是这个

    btn.SetBackgroundColor(Color.Rgb(colors[0],colors[1],colors[2]));
    

    像这样使用它作为你的按钮背景

    btn.setBackgroundResource(R.drawable.button_bg)`
    

    【讨论】:

    • 我没有正确地告诉你你在做什么她并清楚地告诉我你想要什么......你是为每个按钮设置 bg 还是为布局设置 bg 颜色......
    • 我遇到了您的问题,但您不知道如何使用此代码,您能否发布代码如何设置按钮的背景颜色
    【解决方案2】:

    您可以通过编程方式制作 GradientDrawable,通过使用它,您可以在运行时设置边框、颜色。

    这是一个简单的例子。

    public class And_testActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        LinearLayout view = (LinearLayout) findViewById(R.id.content_view);
    
        for( int i=0;i<10;i++) {
            Button btn = new Button(getApplicationContext());
    
            GradientDrawable drawable = new GradientDrawable();
            drawable.setShape(GradientDrawable.RECTANGLE);
            drawable.setStroke(5, Long.decode("0xff00ffff").intValue() + i * 30);
            drawable.setColor(  Long.decode("0xffff00ff").intValue()+ i * 50);
    
            btn.setBackgroundDrawable(drawable);
    
            view.addView(btn , new LayoutParams(30, 30));   
        }
    }
    }
    

    我使用了这个布局 xml。

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

    【讨论】:

    • 非常感谢老兄。它正在工作。但是由于我使用的是 Mono Android,所以我的代码将如最后提到的那样。
    猜你喜欢
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    相关资源
    最近更新 更多