programing tip

획 색상을 사용자 정의 색상으로 변경해야합니다.

itbloger 2020. 12. 28. 07:54
반응형

획 색상을 사용자 정의 색상으로 변경해야합니다. 국가와 관련이 없음


앱에서 획 색상을 변경해야합니다. 사용자가 배경색을 변경할 수 있으므로 버튼의 획 (윤곽선)도 변경하도록해야합니다. 드로어 블 (아래 샘플)에 이미 설정되어 있으므로이를 변경할 방법을 찾지 못했습니다. 이와 같은 다른 모든 질문이 XML 파일을 사용한다고 말한 것처럼 보이지만 .... 이로 인해 동적으로 만들 수 없습니다. 도움을 주셔서 감사합니다!

획 색상을 사용자 정의 색상으로 변경해야합니다. 국가와 관련이 없습니다.

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

    <solid android:color="#ffffffff"/>    
      <stroke
                android:width="3dp"
                android:color="@color/Dim_Gray" />  <<<<--- This is what I need to change


    <padding android:left="10dp"
             android:top="10dp"
             android:right="10dp"
             android:bottom="10dp"
             /> 

    <corners android:bottomRightRadius="12dp" android:bottomLeftRadius="12dp" 
     android:topLeftRadius="12dp" android:topRightRadius="12dp"/> 

</shape>

1. 이와 같은 "뷰"용 드로어 블 파일이있는 경우

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

<corners android:radius="5dp" />

<solid android:color="@android:color/white" />

<stroke
    android:width="3px"
    android:color="@color/blue" />

</shape>

그런 다음 변경할 수 있습니다
. 스트로크 색상 :

GradientDrawable drawable = (GradientDrawable)view.getBackground();
drawable.setStroke(3, Color.RED); // set stroke width and stroke color 


비. 단색 :

GradientDrawable drawable = (GradientDrawable)view.getBackground();
drawable.setColor(Color.RED); // set solid color

2. 이와 같은 "뷰"용 드로어 블 파일이있는 경우

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:id="@+id/buttonSelected">
        <shape>
            <solid android:color="@color/blue" />
            <stroke android:width="1px" android:color="@color/blue" />
        </shape>
    </item>
    <item android:state_checked="false" android:id="@+id/buttonNotSelected">
        <shape android:shape="rectangle">
            <solid android:color="@color/white" />
            <stroke android:width="1px" android:color="@color/blue" />
        </shape>
    </item>
</selector>

그런 다음 위치별로 별도의 드로어 블 개체가져와 개별 항목 속성을 변경할 수 있습니다 .

StateListDrawable drawable = (StateListDrawable)view.getBackground();
DrawableContainerState dcs = (DrawableContainerState)drawable.getConstantState();
Drawable[] drawableItems = dcs.getChildren();
GradientDrawable gradientDrawableChecked = (GradientDrawable)drawableItems[0]; // item 1 
GradientDrawable gradientDrawableUnChecked = (GradientDrawable)drawableItems[1]; // item 2

이제 획 또는 단색변경하려면 :

//solid color
gradientDrawableChecked.setColor(Color.BLUE);
gradientDrawableUnChecked.setColor(Color.RED);

//stroke
gradientDrawableChecked.setStroke(1, Color.RED);
gradientDrawableUnChecked.setStroke(1, Color.BLUE);

GradientDrawable의 폭을 모르고 획 색상을 변경하는 방법이 필요했습니다 . 내 목표는 Drawable.setTint. solidshapexml에 투명 요소 를 추가 해야 작동합니다.

<!-- stroke_background.xml -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="4dp"
        android:color="@android:color/white" />
    <!-- Need transparent solid to get tint applied to only the stroke -->
    <solid android:color="@android:color/transparent"/>
</shape>
// StrokeView.kt
setBackgroundResource(R.drawable.stroke_background)
// Set the color of your stroke
drawable.setTint(Color.BLUE)

귀하의 경우에는, 당신은 모두이 있기 때문에 solid그리고 stroke, 당신은을 사용해야합니다 layer-list스트로크가 (이 위에 그려 그래서) 고체 후에 추가된다. 이렇게하면 획 너비가 무엇인지 미리 알지 않고도 단색과 획에 다른 색상을 설정할 수 있습니다.

<!-- stroke_solid_background.xml -->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/solid_background" />
    <item android:drawable="@drawable/stroke_background" />
</layer-list>
// StrokeSolidView.kt
setBackgroundResource(R.drawable.stroke_solid_background)
(drawable as LayerDrawable).apply {
    // Set the color of your solid
    getDrawable(0).setTint(Color.GREEN)
    // Set the color of your stroke
    getDrawable(1).setTint(Color.BLUE)
}

Please look at the LayerDrawable because it created from your XML and used at runtime.

Here is a Demo Example:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
    <item android:id="@+id/itemId" android:top="-4dp" android:right="-4dp" android:left="-4dp">
        <shape>
            <solid android:color="@android:color/transparent"/>
            <stroke
                android:width="1.5dp"
                android:color="#06C1D7" >
            </stroke>
            <padding
                android:bottom="-2dp"/>

        </shape>
    </item>
</layer-list>

You can modify it at runtime like:

 LayerDrawable layerDrawable = (LayerDrawable) getResources()
                .getDrawable(R.drawable.only_one_bottom_line);
        GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.itemId);
        int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1.5f, getResources().getDisplayMetrics());

        gradientDrawable.setStroke(px, getResources().getColor(R.color.theme_color));
        tv_select_city_name.setBackground(layerDrawable);

Perhaps they are referring to Color State Lists which allows you to change the color based on whether the button was pressed/focused/enabled/etc


Try using StateLists (as opposed to ColorStateList). Take a look: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

You can also create a ShapeDrawable (or a RoundRectShape in your example) programmatically, and then call the button's setBackgroundDrawable


I answered a similar question in Change shape border color at runtime

Its like the same solution proposed by f20k but in my case the drawable was a GradientDrawable instead of a ShapeDrawable.

see if it works...

ReferenceURL : https://stackoverflow.com/questions/4772537/i-need-to-change-the-stroke-color-to-a-user-defined-color-nothing-to-do-with-th

반응형