[英]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.