【问题标题】:How to create corner radius in top left and bottom right in android?如何在android的左上角和右下角创建圆角半径?
【发布时间】:2020-10-27 12:34:17
【问题描述】:

如何在android的左上角和右下角创建圆角半径? 像这样

我的代码是


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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="@drawable/bg_layout">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerInParent="true"
                android:layout_margin="@dimen/twenty_sp"
                android:text="@string/sign_in"
                android:textColor="@color/colorWhite"
                android:textSize="@dimen/thirty_sp" />
        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_layout_white"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/ten_sp"
                    android:orientation="vertical"
                    android:padding="@dimen/ten_sp">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="left"
                        android:text="@string/phone_number"
                        android:textColor="@color/colorBlack" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/five_sp"
                        android:orientation="horizontal">

                        <Spinner
                            android:id="@+id/main_spinner_coutry"
                            android:layout_width="wrap_content"
                            android:layout_height="@dimen/fifty_sp"
                            android:entries="@array/country_code"
                            android:prompt="@array/country_code"
                            android:spinnerMode="dropdown" />

                        <AutoCompleteTextView
                            android:layout_width="match_parent"
                            android:layout_height="@dimen/fifty_sp"
                            android:background="@drawable/bg_edt"
                            android:digits="0123456789"
                            android:ems="10"
                            android:hint="@string/_9876543210"
                            android:imeOptions="actionGo"
                            android:inputType="phone"
                            android:maxLength="10" />
                    </LinearLayout>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/ten_sp"
                    android:orientation="vertical"
                    android:padding="@dimen/ten_sp">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="left"
                        android:text="@string/password"
                        android:textColor="@color/colorBlack" />

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/fifty_sp"
                        android:layout_margin="@dimen/five_sp"
                        android:background="@drawable/bg_edt"
                        android:hint="@string/_9876543210"
                        android:inputType="textPassword" />

                </LinearLayout>


            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/ten_sp"
                android:orientation="vertical"
                android:padding="@dimen/ten_sp">

                <Button
                    android:id="@+id/main_btn_signin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/bg_btn"
                    android:padding="@dimen/ten_sp"
                    android:text="@string/sign_in"
                    android:textAllCaps="false"
                    android:textColor="@color/colorWhite"
                    android:layout_margin="@dimen/ten_sp"/>

                <TextView
                    android:layout_margin="@dimen/ten_sp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="@dimen/ten_sp"
                    android:text="@string/forgot_password"
                    android:textColor="@color/colorAccent" />

            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

背景可绘制是

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle">
    <solid android:color="@color/colorWhite" />
    <corners
        android:topRightRadius="30dp"
         />

</shape>

显示为图片我想在代码上方的左上角和右下角工作,但它只显示一个带有背景颜色的角,我尝试创建上面的图片,但它不能工作......

【问题讨论】:

    标签: java android layout material-components-android android-shapedrawable


    【解决方案1】:

    使用材料组件库,您可以定义自定义CornerTreatment
    例如,您可以使用 CardView 并向其应用 ShapeAppearanceModel

    <LinearLayout
        android:background="@color/colorPrimaryLight"
        android:clipChildren="false"
        android:clipToPadding="false"
        ..>
    
       <com.google.android.material.card.MaterialCardView
           android:id="@+id/card"
           app:cardCornerRadius="32dp"
           app:cardBackgroundColor="@color/colorPrimaryDark"
           ../>
    
    </LinearLayout>
    

    然后:

    MaterialCardView cardView = findViewById(R.id.card);
            
    cardView.setShapeAppearanceModel(cardView.getShapeAppearanceModel().toBuilder()
                    .setTopLeftCorner(new CrazyCornerTreatment())
                    .setBottomLeftCorner(CornerFamily.ROUNDED,0f)
                    .setBottomRightCorner(CornerFamily.ROUNDED,0f)
                    .build());
    

    左上角可以定义为:

    class CrazyCornerTreatment : CornerTreatment() {
    
        override fun getCornerPath(
                shapePath: ShapePath,
                angle: Float,
                interpolation: Float,
                radius: Float
        ) {
    
            val interpolatedRadius = radius * interpolation
            shapePath.reset(0f, -radius * interpolation, 270f,270 -angle)
            shapePath.addArc(
                    0f,
                    -2*interpolatedRadius,
                    2*interpolatedRadius,
                    0f,
                    180f,
                     - angle)
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      你需要创建一个可绘制的形状

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android">
      <solid android:color="#5089fa" />
      <size
          android:width="82dp"
          android:height="82dp" />
      <padding
          android:bottom="10dp"
          android:left="10dp"
          android:right="10dp"
          android:top="10dp" />
      <corners
          android:bottomLeftRadius="0dp"
          android:bottomRightRadius="8dp"
          android:topLeftRadius="8dp"
          android:topRightRadius="0dp" />
      

      将该可绘制形状设置为您的背景,它会产生如下结果:

      Odd Rounded Corners

      【讨论】:

        【解决方案3】:

        可绘制的形状是一个很好的解决方案。 您也可以稍微重构它并使用顶部小部件的左下角和第二个小部件的右上角。 如果背景在顶部为白色,在其下方为紫色,则仅使用“正常”角半径属性即可获得所需的结果。

        【讨论】:

          【解决方案4】:

          创建具有两种颜色(即白色和紫色)​​的可绘制背景

          layout_background.xml

          <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
          <item android:bottom="300dp">
              <shape android:shape="rectangle" >
                  <size android:height="300dp" />
                  <solid android:color="#ffffff" />
              </shape>
          </item>
          
          <item android:top="300dp">
              <shape android:shape="rectangle" >
                  <size android:height="50dp" />
                  <solid android:color="##2d39a1" />
              </shape>
          </item>
          

          将此可绘制对象设置为布局的背景

           <?xml version="1.0" encoding="utf-8"?>
          <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              tools:context=".MainActivity">
          
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="@drawable/layout_background"
                  android:orientation="vertical"
                  tools:context=".MainActivity">
          
                  <RelativeLayout
                      android:layout_width="match_parent"
                      android:layout_height="300dp"
                      android:background="@drawable/purple_background">
          
                      <TextView
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_alignParentBottom="true"
                          android:layout_centerInParent="true"
                          android:layout_margin="@dimen/twenty_sp"
                          android:text="@string/sign_in"
                          android:textColor="@color/colorWhite"
                          android:textSize="@dimen/thirty_sp" />
                  </RelativeLayout>
          
                  <LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:background="@drawable/white_background"
                      android:orientation="vertical">
          
                      <LinearLayout
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:orientation="vertical">
          
                          <LinearLayout
                              android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:layout_margin="@dimen/ten_sp"
                              android:orientation="vertical"
                              android:padding="@dimen/ten_sp">
          
                              <TextView
                                  android:layout_width="match_parent"
                                  android:layout_height="wrap_content"
                                  android:gravity="left"
                                  android:text="@string/phone_number"
                                  android:textColor="@color/colorBlack" />
          
                              <LinearLayout
                                  android:layout_width="match_parent"
                                  android:layout_height="wrap_content"
                                  android:layout_margin="@dimen/five_sp"
                                  android:orientation="horizontal">
          
                                  <Spinner
                                      android:id="@+id/main_spinner_coutry"
                                      android:layout_width="wrap_content"
                                      android:layout_height="@dimen/fifty_sp"
                                      android:entries="@array/country_code"
                                      android:prompt="@array/country_code"
                                      android:spinnerMode="dropdown" />
          
                                  <AutoCompleteTextView
                                      android:layout_width="match_parent"
                                      android:layout_height="@dimen/fifty_sp"
                                      android:background="@drawable/bg_edt"
                                      android:digits="0123456789"
                                      android:ems="10"
                                      android:hint="@string/_9876543210"
                                      android:imeOptions="actionGo"
                                      android:inputType="phone"
                                      android:maxLength="10" />
                              </LinearLayout>
          
                          </LinearLayout>
          
                          <LinearLayout
                              android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:layout_margin="@dimen/ten_sp"
                              android:orientation="vertical"
                              android:padding="@dimen/ten_sp">
          
                              <TextView
                                  android:layout_width="match_parent"
                                  android:layout_height="wrap_content"
                                  android:gravity="left"
                                  android:text="@string/password"
                                  android:textColor="@color/colorBlack" />
          
                              <EditText
                                  android:layout_width="match_parent"
                                  android:layout_height="@dimen/fifty_sp"
                                  android:layout_margin="@dimen/five_sp"
                                  android:background="@drawable/bg_edt"
                                  android:hint="@string/_9876543210"
                                  android:inputType="textPassword" />
          
                          </LinearLayout>
          
          
                      </LinearLayout>
          
                      <LinearLayout
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:layout_margin="@dimen/ten_sp"
                          android:orientation="vertical"
                          android:padding="@dimen/ten_sp">
          
                          <Button
                              android:id="@+id/main_btn_signin"
                              android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:background="@drawable/bg_btn"
                              android:padding="@dimen/ten_sp"
                              android:text="@string/sign_in"
                              android:textAllCaps="false"
                              android:textColor="@color/colorWhite"
                              android:layout_margin="@dimen/ten_sp"/>
          
                          <TextView
                              android:layout_margin="@dimen/ten_sp"
                              android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:gravity="center"
                              android:padding="@dimen/ten_sp"
                              android:text="@string/forgot_password"
                              android:textColor="@color/colorAccent" />
          
                      </LinearLayout>
                  </LinearLayout>
              </LinearLayout>
          </ScrollView>
          

          purple_background.xml

              <?xml version="1.0" encoding="utf-8"?>
          <shape xmlns:android="http://schemas.android.com/apk/res/android"
              android:padding="10dp"
              android:shape="rectangle">
              <solid android:color="@color/colorPurple" />
              <corners
                  android:bottomLeftRadius="30dp"
                   />
          
          </shape>
          

          white_background.xml

          <?xml version="1.0" encoding="utf-8"?>
          <shape xmlns:android="http://schemas.android.com/apk/res/android"
              android:padding="10dp"
              android:shape="rectangle">
              <solid android:color="@color/colorWhite" />
              <corners
                  android:topRightRadius="30dp"
                   />
          
          </shape>
          

          如果背景不合适,请在 layout_background.xml 文件中调整高度。我希望你明白了..

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-06-08
            • 2012-09-13
            • 2021-01-13
            • 1970-01-01
            • 1970-01-01
            • 2013-05-10
            • 2017-01-18
            相关资源
            最近更新 更多