[英]How to create corner radius in top left and bottom right in android?
如何在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>
显示为图片我想在代码上方的左上角和右下角工作,但它只显示一个带有背景颜色的角,我尝试创建上面的图片,但它不能工作......
您需要创建一个可绘制的形状
<?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" />
将该可绘制形状设置为您的背景,它会产生如下结果:
可绘制的形状是一个非常好的解决方案。 您也可以稍微重构它并使用顶部小部件的左下角和第二个小部件的右上角。 如果背景在顶部为白色,在其下方为紫色,则您将获得所需的结果,同时仅使用“正常”角半径属性。
使用材料组件库,您可以定义自定义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)
}
}
使用两个 colors 即白色和紫色创建可绘制的背景
<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>
<?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>
<?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 文件中的高度。 我希望你明白了。。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.