代码之家  ›  专栏  ›  技术社区  ›  CHarris

如何通过编程设置文本视图的边框颜色?

  •  0
  • CHarris  · 技术社区  · 7 年前

    我正试图改变我衣服的边框颜色 TextView 以编程方式,取决于 if 我的活动状况。这是我的 ViewContact.java

    if (bob == 0) {
      //change colour depending on value
    
      LayerDrawable layerDrawable = (LayerDrawable) ContextCompat
          .getDrawable(ViewContact.this,R.drawable.textboxes);
      GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable
          .findDrawableByLayerId(R.id.textbox_shape);
      gradientDrawable.setColor(Color.parseColor("#DA850B")); // change color
    }
    
    if (bob == 1) {
    
      LayerDrawable layerDrawable = (LayerDrawable) ContextCompat
          .getDrawable(ViewContact.this,R.drawable.textboxes);
      GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable
          .findDrawableByLayerId(R.id.textbox_shape);
      gradientDrawable.setColor(Color.parseColor("#0A7FDA")); // change color
    
    }
    
    if (bob == 2) {
    
    etc...
    
    }
    

    我看了这里 Changing color in a shape inside a layer-list programmatically 这里呢 Android change color stroke (border) programmatically 和其他地方寻求解决方案,但无法使其发挥作用。

    这是你的电话号码 xml 为了我的 ViewContact.java 活动 activity_view_contact.xml :

            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight=".5"
                android:background="@drawable/textboxes"
                />
    

    这是 textboxes.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <!--space between each text box-->
        <item
            android:top="3dp"
            android:id="@+id/textbox_shape"
            >
            <shape android:shape="rectangle">
                <!--formatting of colour inside the box-->
                <solid android:color="@android:color/transparent" />
                <!--formatting of lines on the box-->
                <stroke
                    android:width="1dip"
                     />
                <corners android:radius="3dp"/>
                <!--text formatting in the box-->
                <padding
                    android:textSize="30dp"
                    android:top="15dp"
                    android:bottom="1dp"
                    android:left="20dp"
                    android:right="20dp"/>
            </shape>
        </item>
    </layer-list>
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Cheticamp    7 年前

    您正在更改加载的可绘制图形的颜色,但不更改中使用的颜色 TextView . 在代码中添加以下行以在 文本框

    findViewById(R.id.textView1).setBackground(layerDrawable);
    

    您还可以获取 直接与

    LayerDrawable layerDrawable = (LayerDrawable) findViewById(R.id.textView1).getBackground();
    

    以下是一组更改背景和笔划颜色的代码:

    LayerDrawable layerDrawable = (LayerDrawable) findViewById(R.id.textView1).getBackground();
    GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable
        .findDrawableByLayerId(R.id.textbox_shape);
    // Change background color
    gradientDrawable.setColor(Color.parseColor("#DA850B"));
    // Change stroke color. (Assumes 5px stroke width.)
    gradientDrawable.setStroke(5, Color.parseColor("#FF0000"));
    
        2
  •  1
  •   brian.clear    6 年前

    TextView-在XML或代码中添加边框

    创建新的可绘制图形

    • 打开 物件 文件夹

    • 打开

    • 右键单击drawable

    • 选择 刚出现的 &燃气轮机; 可绘制资源文件

    • 新资源屏幕出现

    • textview_border.xml

    • 别管其他的选择

    • 好啊

    创建默认的可绘制图形

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
    </selector>
    

    替换根目录<选择器>用这个<形状/>。。xml,但保留Android Studio生成的确切名称空间-如果与以下内容不同:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!--FILL    -->
        <solid android:color="#fafbfb" />
    
        <!--    BORDER-->
        <stroke android:width="1dp"
                android:color="#FFFFFF" />
    
    </shape>
    

    现在可以将此可绘制形状用作背景

    给定任何文本视图

    在这里,我们将背景颜色设置为XML中的Android系统白色

    android:background="@android:color/white"
    
    <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:background="@android:color/white"
            android:text="SOME TEXT"
            android:textAlignment="center"
            android:textColor="@color/colorAppColorAppDark0"
            android:textSize="@dimen/app_textSize_mediumToSmall"
           .../>
    

    现在我们可以改用可拉伸的

    android:background="@drawable/textview_border"
    
    <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:background="@drawable/textview_border"
            android:text="SOME TEXT"
            android:textAlignment="center"
            android:textColor="@color/colorAppColorAppDark0"
            android:textSize="@dimen/app_textSize_mediumToSmall"
           ..layout commands etc./>
    

    或者您可以通过编程方式添加它

    TextView textView1 = activity.findViewById(R.id.textView1);
    
    //--------------------------------------------------------------------------------------
    //see res/drawable/textview_border.xml
    textView1.setBackgroundResource(R.drawable.textview_border);
    

    • 在设计模式中

    • 列表项

    • 点击你的文本视图

    • 在声明的属性中查找背景或向下滚动到所有属性以添加背景。
    • 在行的最右侧,单击字段右侧的小条
    • 出现背景选择器屏幕
    • 在左侧选择类型:可绘制而不是颜色
    • 查找文本视图\u边框
    • 按OK

    enter image description here

    设定 android:background=“@drawable/textview\u border”

    <TextView
            android:id="@+id/fixture_list_constraintlayout_textView_fixturetypecode"
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:background="@drawable/textview_border"
            android:text="XXX"
            android:textAlignment="center"
            android:textColor="@android:color/white"
            android:textSize="@dimen/app_textSize_mediumToSmall"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        3
  •  0
  •   LundinCast    7 年前

    要更改笔划颜色(边框):

    GradientDrawable gradientDrawable = (GradientDrawable)btn_submit.getBackground();
    gradientDrawable.setStroke(2, Color.GREEN);
    

    它起作用了。。检查一下。。