【问题标题】:android xml drawable half heightandroid xml可绘制半高
【发布时间】:2020-04-03 07:22:24
【问题描述】:

所以我试图实现图层形状占高度的一半。 这是它的外观:

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

<?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>

【问题讨论】:

    标签: java android xml android-studio


    【解决方案1】:

    更改最后一项 android:right 属性

    【讨论】:

      【解决方案2】:

      我想你想要这样。

      <item android:left="50dip">
          <layer-list>
              <item >
                  <shape android:shape="rectangle">
                      <solid android:color="@android:color/white"></solid>
                  </shape>
              </item>
              <item android:height="350dip">
                  <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>
      

      【讨论】:

      • 请详细说明什么您的代码更改以及为什么您认为这是 OP 正在寻找的。​​span>
      • 这是一个固定大小,不是高度的一半;)
      • 你想要绿色和白色的一半大小,这样你就可以根据需要更改项目的高度。
      【解决方案3】:

      如果你的意思是屏幕高度的一半,你不能在 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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-13
        • 1970-01-01
        • 2021-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-28
        • 1970-01-01
        相关资源
        最近更新 更多