Java android xml可绘制半高

Java android xml可绘制半高,java,android,xml,android-studio,Java,Android,Xml,Android Studio,所以我试图实现层的形状是高度的一半。 这就是它的样子: 我希望白色和绿色正好是高度的一半这是代码: <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:left="50dip"> <layer-list>

所以我试图实现层的形状是高度的一半。 这就是它的样子:

我希望白色和绿色正好是高度的一半这是代码:

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

    <item android:left="50dip">
        <layer-list>
            <item android:top="50dip">
                <shape android:shape="rectangle">
                    <solid android:color="@android:color/white"></solid>
                </shape>
            </item>
            <item android:bottom="50dip">
                <shape android:shape="rectangle">
                    <solid android:color="@android:color/holo_green_light"></solid>
                </shape>
            </item>
        </layer-list>
    </item>
    <item android:right="150dip">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="#ff0000" />
        </shape>
    </item>

</layer-list>


更改最后一项android:right属性

更改最后一项android:right属性

我想你想要这样



我想你想要这样



如果您的意思是屏幕高度的一半,则不能在XML中更改,而是在类似这样的代码中更改,并且在应用程序运行时会更新:

//Getting the height of the screen
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;

LayerDrawable layerDrawable = (LayerDrawable) getResources().getDrawable(R.drawable.test);
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.green_shape);
gradientDrawable.setBounds(0,0, 0, height/2); //setbottom bound to be half of the height
以及xml:

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

    <item android:left="50dip">
        <layer-list>
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@android:color/white"></solid>
                </shape>
            </item>
            <item android:id="@+id/green_shape">
                <shape android:shape="rectangle">
                    <solid android:color="@android:color/holo_green_light"></solid>
                </shape>
            </item>
        </layer-list>
    </item>
    <item android:right="150dip">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="#ff0000" />
        </shape>
    </item>

</layer-list>

如果您的意思是屏幕高度的一半,则不能在XML中更改,而是在类似这样的代码中更改,并且在应用程序运行时会更新:

//Getting the height of the screen
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;

LayerDrawable layerDrawable = (LayerDrawable) getResources().getDrawable(R.drawable.test);
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.green_shape);
gradientDrawable.setBounds(0,0, 0, height/2); //setbottom bound to be half of the height
以及xml:

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

    <item android:left="50dip">
        <layer-list>
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@android:color/white"></solid>
                </shape>
            </item>
            <item android:id="@+id/green_shape">
                <shape android:shape="rectangle">
                    <solid android:color="@android:color/holo_green_light"></solid>
                </shape>
            </item>
        </layer-list>
    </item>
    <item android:right="150dip">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="#ff0000" />
        </shape>
    </item>

</layer-list>


请详细说明您的代码发生了什么变化,以及为什么您认为这是OP正在寻找的。这是一个固定大小,而不是高度的一半;)您需要绿色和白色的一半大小,以便可以根据需要更改项目的高度。请详细说明您的代码更改了什么,以及为什么您认为这是OP正在寻找的。这仍然是一个固定大小,而不是高度的一半;)您需要一半大小的绿色和白色,以便可以根据需要更改项目的高度。