具有OVAL形状的GradientDrawable在以编程方式添加的Text View中无法正常工作 [英] GradientDrawable with OVAL shape not working inside programatically added Text View

查看:124
本文介绍了具有OVAL形状的GradientDrawable在以编程方式添加的Text View中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以编程方式添加了Text ViewButton.

代码

final TextView test = new TextView(getApplicationContext());
test.setText(String.valueOf(counterQuantity));
test.setTextColor(getResources().getColor(R.color.black));
test.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);

GradientDrawable border = new GradientDrawable();
border.setShape(GradientDrawable.OVAL);
border.setStroke(1, getResources().getColor(R.color.black));
border.setCornerRadius(2);
border.setColor(getResources().getColor(R.color.colorPrimaryDark)); //white background
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
  test.setBackgroundDrawable(border);
} else {
  test.setBackground(border);
}

Button articleButtonId = (Button) v;
int articleButton = articleButtonId.getId();
final String articleButtonText = articleButtonId.getText().toString();
Log.w("articleButton", "" + articleButton);

final Button button = new Button(getApplicationContext());
button.setText(test.getText() + "  " + "  " + articleButtonText);
button.setGravity(Gravity.CENTER);
button.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
button.setTextColor(getResources().getColor(R.color.black));
button.setBackgroundColor(getResources().getColor(R.color.article_button_group));
button.setBackground(getResources().getDrawable(R.drawable.order_button_group_bg));

在上面的代码中,我以编程方式添加了Text ViewButton.

Button setText方法内使用叠加法添加的Text Views文本.

现在我只想在Circle View.

中制作Text View

我尝试过使用GradientDrawable,但是对Text View的效果仍然相同,没有Text View产生的任何效果.

在其他情况下GradientDrawable可以正常工作,但在这种情况下无法获得我想要的东西.

这里是我想要的图像.

我们将不胜感激任何帮助.

解决方案

下面是您的问题的摘要,我已经在kitkat和棉花糖设备上进行了测试,显示正确,请尝试一下...

activity_main.xml

<LinearLayout
    android:id="@+id/lv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"/>

MainActivity.java

1-查找linearlayout的ID

2-然后使用TextView或Button等创建动态视图...

private void createDynamicView() {
    TextView tvDigit = new TextView(this);
    tvDigit.setText("1");
    tvDigit.setTextColor(ContextCompat.getColor(this, R.color.white));
    tvDigit.setGravity(Gravity.CENTER);
    tvDigit.setTextSize(18);
    customViewOval(tvDigit, ContextCompat.getColor(this, R.color.teal_500), ContextCompat.getColor(this, R.color.transparant), 100, 100);

    TextView tvName = new TextView(this);
    tvName.setText("Richard");
    tvName.setGravity(Gravity.CENTER);
    tvName.setTextColor(ContextCompat.getColor(this, R.color.black_80));
    tvName.setPadding(10, 0, 0, 0);
    tvName.setTextSize(18);

    lv.addView(tvDigit);
    lv.addView(tvName);

    customViewWithRadius(lv, ContextCompat.getColor(this, R.color.lightgrey), ContextCompat.getColor(this, R.color.transparant), 90);
}

椭圆形内数字的自定义视图

public static void customViewOval(View view, int backgroundColor, int borderColor, int height, int width) {
    GradientDrawable shape = new GradientDrawable();
    shape.setShape(GradientDrawable.OVAL);
    shape.setColor(backgroundColor);
    shape.setStroke(1, borderColor);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(height, width);
    view.setLayoutParams(layoutParams);
    view.setBackgroundDrawable(shape);
}

具有半径的主要背景的自定义视图

private static void customViewWithRadius(View view, int backgroundColor, int borderColor, float radius) {
    GradientDrawable shape = new GradientDrawable();
    shape.setShape(GradientDrawable.RECTANGLE);
    shape.setCornerRadii(new float[]{radius, radius, radius, radius, radius, radius, radius, radius});
    shape.setColor(backgroundColor);
    shape.setStroke(1, borderColor);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    view.setLayoutParams(layoutParams);
    view.setPadding(10, 10, 40, 10);
    view.setBackgroundDrawable(shape);
}

希望这将有助于解决您的问题.

I have programmatically added Text View and Button.

Code

final TextView test = new TextView(getApplicationContext());
test.setText(String.valueOf(counterQuantity));
test.setTextColor(getResources().getColor(R.color.black));
test.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);

GradientDrawable border = new GradientDrawable();
border.setShape(GradientDrawable.OVAL);
border.setStroke(1, getResources().getColor(R.color.black));
border.setCornerRadius(2);
border.setColor(getResources().getColor(R.color.colorPrimaryDark)); //white background
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
  test.setBackgroundDrawable(border);
} else {
  test.setBackground(border);
}

Button articleButtonId = (Button) v;
int articleButton = articleButtonId.getId();
final String articleButtonText = articleButtonId.getText().toString();
Log.w("articleButton", "" + articleButton);

final Button button = new Button(getApplicationContext());
button.setText(test.getText() + "  " + "  " + articleButtonText);
button.setGravity(Gravity.CENTER);
button.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
button.setTextColor(getResources().getColor(R.color.black));
button.setBackgroundColor(getResources().getColor(R.color.article_button_group));
button.setBackground(getResources().getDrawable(R.drawable.order_button_group_bg));

Here in above code i have a Text View and Button added programatically.

The text of Text Views added inside Button setText method using concatination.

Now i want to make only Text View in Circle View.

I have tried with GradientDrawable but the effect on Text View is remains same no any effects taken by Text View.

In other scenarios GradientDrawable is working fine but in this scenario is not getting what i want.

Here is image what i want exactly.

Any help will be highly appreciated.

解决方案

below is a snippet for your problem and I've tested on kitkat and marshmallow devices, it is showing properly, So give it a try...

activity_main.xml

<LinearLayout
    android:id="@+id/lv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"/>

MainActivity.java

1- find id of linearlayout

2 - Then create dynamic view with TextView or Button etc...

private void createDynamicView() {
    TextView tvDigit = new TextView(this);
    tvDigit.setText("1");
    tvDigit.setTextColor(ContextCompat.getColor(this, R.color.white));
    tvDigit.setGravity(Gravity.CENTER);
    tvDigit.setTextSize(18);
    customViewOval(tvDigit, ContextCompat.getColor(this, R.color.teal_500), ContextCompat.getColor(this, R.color.transparant), 100, 100);

    TextView tvName = new TextView(this);
    tvName.setText("Richard");
    tvName.setGravity(Gravity.CENTER);
    tvName.setTextColor(ContextCompat.getColor(this, R.color.black_80));
    tvName.setPadding(10, 0, 0, 0);
    tvName.setTextSize(18);

    lv.addView(tvDigit);
    lv.addView(tvName);

    customViewWithRadius(lv, ContextCompat.getColor(this, R.color.lightgrey), ContextCompat.getColor(this, R.color.transparant), 90);
}

custom view for digit within oval

public static void customViewOval(View view, int backgroundColor, int borderColor, int height, int width) {
    GradientDrawable shape = new GradientDrawable();
    shape.setShape(GradientDrawable.OVAL);
    shape.setColor(backgroundColor);
    shape.setStroke(1, borderColor);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(height, width);
    view.setLayoutParams(layoutParams);
    view.setBackgroundDrawable(shape);
}

custom view for main background with radius

private static void customViewWithRadius(View view, int backgroundColor, int borderColor, float radius) {
    GradientDrawable shape = new GradientDrawable();
    shape.setShape(GradientDrawable.RECTANGLE);
    shape.setCornerRadii(new float[]{radius, radius, radius, radius, radius, radius, radius, radius});
    shape.setColor(backgroundColor);
    shape.setStroke(1, borderColor);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    view.setLayoutParams(layoutParams);
    view.setPadding(10, 10, 40, 10);
    view.setBackgroundDrawable(shape);
}

Hope it'll help to solve your problem.

这篇关于具有OVAL形状的GradientDrawable在以编程方式添加的Text View中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆