-1

我以编程方式添加了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 View并以Button编程方式添加。

Text Views使用连接添加的内部Button setText方法的文本。

现在我只想Text ViewCircle View.

我已经尝试过,GradientDrawable但效果Text View仍然相同,没有任何效果Text View

在其他情况下GradientDrawable工作正常,但在这种情况下没有得到我想要的。

这是我想要的图像。

在此处输入图像描述

任何帮助将不胜感激。

4

1 回答 1

2

下面是您的问题的一个片段,我已经在 kitkat 和 marshmallow 设备上进行了测试,它显示正确,所以试一试......

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-查找线性布局的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);
}

希望它有助于解决您的问题。

于 2016-12-05T11:43:41.727 回答