【问题标题】:Half circle shape not work半圆形不工作
【发布时间】:2016-10-14 12:37:27
【问题描述】:

我尝试创建半圆形背景,在development IDE preview 中它可以工作,但是当我在模拟器中启动时它不起作用。

这是我的形状代码:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:height="50dp">
        <shape>
            <solid android:color="@color/colorAccentDark" />
        </shape>
    </item>
    <item android:top="-500dp" android:bottom="0dp" android:left="-100dp" android:right="-100dp">
        <shape>
            <corners android:radius="500dp" />
            <solid android:color="@color/colorAccentDark" />
        </shape>
    </item>
</layer-list>

这是我的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_profile"
        android:layout_weight="1">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/profile_avatar"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/ic_default_avatar"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="50dp"/>

        <TextView
            android:id="@+id/profile_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/profile_avatar"
            android:layout_marginTop="20dp"
            android:textColor="@color/neutralWhite"
            android:textStyle="bold"
            android:textSize="18sp"
            android:text="Avatar Ang"/>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="4.04">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello Android!"/>

    </RelativeLayout>

</LinearLayout>

也许有任何其他的调整来处理这个问题? 谢谢

【问题讨论】:

    标签: android drawable shape


    【解决方案1】:

    你可以试试这个:

    <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="#900021df"/>
        <size
            android:width="10dp"
            android:height="5dp"/>
        <corners
            android:bottomLeftRadius="20dp"
            android:bottomRightRadius="20dp"/>
    </shape>
    

    它给出了这个形状:

    【讨论】:

      【解决方案2】:

      curve_toolbar_bg.xml

      <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
          <item>
              <shape android:shape="rectangle"/>
          </item>
          <item
              android:bottom="0dp"
              android:left="-100dp"
              android:right="-100dp"
              android:top="-80dp">
              <shape android:shape="oval">
                  <solid android:color="@color/colorPrimary" />
              </shape>
          </item>
      </layer-list>
      

      activity_main.xml

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/container"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <android.support.v7.widget.Toolbar
              android:id="@+id/toolbar"
              android:layout_width="0dp"
              android:layout_height="?android:attr/actionBarSize"
              android:background="@drawable/rounded_corner"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintVertical_bias="0.0">
      
          </android.support.v7.widget.Toolbar>
      </android.support.constraint.ConstraintLayout>
      

      【讨论】:

      • 谢谢。这也适用于底部导航、文本视图等其他视图
      【解决方案3】:

      您不能从 xml 创建半圆。但是您可以使用具有适当边距和填充的圆圈来实现您正在寻找的东西。

      您可以使用圆形 .xml 文件。 像这样创建一个固定大小的圆圈:

      例子:

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
             android:shape="oval"
             android:useLevel="false" >
         <solid android:color="#006AC5" />
         <size
          android:height="50dp"
          android:width="50dp" />
      </shape>
      

      【讨论】:

      • @HardikJoshi 完整阅读了我的答案You cannot create a semicircle from xml. but you could achieve what you are looking for using a circle with appropriate margin &amp; padding.
      • 它的作品。形状会影响性能吗?
      【解决方案4】:

      试试这个。删除渐变部分,然后它看起来就像你想要的一样。

      <item>
          <shape xmlns:android="http://schemas.android.com/apk/res/android"
              android:shape="rectangle">
              <solid android:color="#00B0EA" />
      
          </shape>
      </item>
      <item
          android:bottom="400dp"
          android:left="-100dp"
          android:right="-100dp"
          android:top="-200dp">
          <shape xmlns:android="http://schemas.android.com/apk/res/android"
              android:shape="oval">
              <gradient
                  android:angle="90"
                  android:endColor="#65FFFFFF"
                  android:startColor="#65FFFFFF" />
          </shape>
      </item>
      <item
          android:bottom="402dp"
          android:left="-100dp"
      
          android:right="-100dp"
          android:top="-280dp">
          <shape xmlns:android="http://schemas.android.com/apk/res/android"
              android:shape="oval">
              <solid android:color="#FFFFFF" />
          </shape>
      </item>
      

      【讨论】:

        【解决方案5】:

        Background.xml


        <?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="#FFFFFF" />
                </shape>
            </item>
            <item
                android:bottom="410dp"
                android:left="-100dp"
                android:right="-100dp"
                android:top="-300dp">
                <shape xmlns:android="http://schemas.android.com/apk/res/android"
                    android:shape="oval">
        
                    <solid android:color="#7181A1" />
                </shape>
            </item>
        </layer-list>
        

        Output like

        【讨论】:

          【解决方案6】:

          您可以尝试使用ShapeOfView

          <io.github.florent37.shapeofview.shapes.ArcView
              android:layout_width="match_parent"
              android:layout_height="300dp"
              android:elevation="4dp"
              app:shape_arc_height="20dp"
              app:shape_arc_position="bottom">
          
              <!-- YOUR CONTENT -->
          
          </io.github.florent37.shapeofview.shapes.ArcView>
          

          Image result

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-04-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-10-09
            相关资源
            最近更新 更多