3

ImageViewプログラムでにいくつか追加していますがLinearLayout、それらImageViewは にsrc設定されていR.drawable.rectangleます。

左だけに境界線がある単色の長方形を作成しようとしています。

R.drawable.rectangleは次のようになります。

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
            <solid android:color="#000000" />
            <stroke android:width="1dp" android:color="#999" />
        </shape>
    </item>
    <item 
        android:top="0dp" android:left="1dp" android:bottom="0dp" android:right="0dp">
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
            <solid android:color="#283BCB"/>
            <size android:width="100dp" android:height="8dp" />
        </shape>
    </item>
</layer-list>

私のアクティビティでは、を含むレイアウトを膨らませており、LinearLayoutImageViews をプログラムで追加しています。これはすべて機能しています。問題は、長方形の幅を操作できないことです。私が行った場合:

imageView.getLayoutParams().width = 10;

長方形は、異なるサイズと色である必要があります。ただし、このようにすると、長方形が奇妙に変形します。レイヤーリストを使用せずに形状のみを使用すると、これは起こりません(ただし、その方法では左の境界線を追加できません)。

だから私はLayerDrawableそれを取得GradientDrawableしてから変更する必要があると推測しています。しかし、私はこれを達成する運がありません。

LayerDrawable layer = (LayerDrawable)imageView.getDrawable();
GradientDrawable rectangle = (GradientDrawable)layer.getDrawable(1);
// rectangle.setBounds
// rectangle.setColor NOT working

ヒントはありますか?ありがとう

4

0 に答える 0