단추의 모서리를 둥글게 만드는 방법은?
나는 구석진 곳을 만들고 싶습니다.button
라운드. 안드로이드에서 이것을 달성할 수 있는 쉬운 방법이 있습니까?
만약 당신이 이런 것을 원한다면,
여기 코드가 있습니다.
1. mybutton.xml과 같은 그리기 가능한 폴더에 xml 파일을 만들고 다음 마크업을 붙여넣습니다.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#345953" android:endColor="#689a92" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<solid android:color="#58857e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />
</shape>
</item>
</selector>
2.이제 보기의 배경에 이 그림표를 사용합니다.보기가 단추인 경우 다음과 같은 작업을 수행합니다.
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#ffffff"
android:background="@drawable/mybutton"
android:text="Buttons" />
아래와 같은 그리기 가능한 폴더에 xml 파일 만들기
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<solid android:color="#ABABAB"/>
<corners android:radius="10dp"/>
</shape>
모서리를 둥글게 만들고자 하는 버튼에 배경으로 적용합니다.
또는 아래와 같이 모든 모서리에 대해 별도의 반경을 사용할 수 있습니다.
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
안드로이드에서 이를 달성할 수 있는 쉬운 방법이 있습니까?
Jetpack Composite를 사용하면 다음 파라미터를 사용할 수 있습니다.
Button(
onClick = { /* Do something! */ },
shape = RoundedCornerShape(8.dp)
){
Text("Button")
}
재료 구성요소 라이브러리에서 특성과 함께 를 사용할 수 있습니다.
다음과 같은 것:
<com.google.android.material.button.MaterialButton
android:text="BUTTON"
app:cornerRadius="8dp"
../>
모서리가 둥근 버튼을 얻기에 충분합니다.
재료 단추 스타일 중 하나를 사용할 수 있습니다.예:
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
.../>
또한 버전 1.1.0부터는 단추의 모양을 변경할 수 있습니다.버튼 스타일의 속성을 사용하면 됩니다.
<style name="MyButtonStyle" parent="Widget.MaterialComponents.Button">
<item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Button.Rounded</item>
</style>
<style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">16dp</item>
</style>
그러면 다음을 사용합니다.
<com.google.android.material.button.MaterialButton
style="@style/MyButtonStyle"
.../>
다음을 적용할 수도 있습니다.shapeAppearanceOverlay
레이아웃xml 파일:
<com.google.android.material.button.MaterialButton
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MyApp.Button.Rounded"
.../>
그shapeAppearance
또한 각 모서리마다 모양과 치수가 다를 수 있습니다.
<style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerFamilyTopRight">cut</item>
<item name="cornerFamilyBottomRight">cut</item>
<item name="cornerSizeTopLeft">32dp</item>
<item name="cornerSizeBottomLeft">32dp</item>
</style>
아래와 같은 XML 파일을 만듭니다.버튼의 배경으로 설정합니다.버튼에 추가 곡선이 필요한 경우 원하는 대로 반지름 속성을 변경합니다.
button_background.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/primary" />
<corners android:radius="5dp" />
</shape>
단추에 배경을 설정합니다.
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_background"/>
그리기 가능한 폴더에 shape.xml 생성
like shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="2dp"
android:color="#FFFFFF"/>
<gradient
android:angle="225"
android:startColor="#DD2ECCFA"
android:endColor="#DD000000"/>
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
그리고 myactivity.xml에서
사용할 수 있습니다.
<Button
android:id="@+id/btn_Shap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Shape"
android:background="@drawable/shape"/>
myButton.xml 파일 만들기
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorButton"/>
<corners android:radius="10dp"/>
</shape>
단추에 추가
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/myButton"/>
제가 알아낸 간단한 방법은 그리기 가능한 폴더에 새 xml 파일을 만든 다음 버튼 배경을 해당 xml 파일로 지정하는 것이었습니다.내가 사용한 코드는 다음과 같습니다.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ff8100"/>
<corners android:radius="5dp"/>
</shape>
Drawable 폴더에 rounded_btn.xml 파일 생성...
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/#FFFFFF"/>
<stroke android:width="1dp"
android:color="@color/#000000"
/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"
/>
<corners android:bottomRightRadius="5dip" android:bottomLeftRadius="5dip"
android:topLeftRadius="5dip" android:topRightRadius="5dip"/>
</shape>
이 .xml 파일을 단추 배경으로 사용합니다.
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_btn"
android:text="Test" />
아이콘이 있는 스타일 단추
<Button
android:id="@+id/buttonVisaProgress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:background="@drawable/shape"
android:onClick="visaProgress"
android:drawableTop="@drawable/ic_1468863158_double_loop"
android:padding="10dp"
android:text="Visa Progress"
android:textColor="@android:color/white" />
shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="14dp" />
<gradient
android:angle="45"
android:centerColor="#1FA8D1"
android:centerX="35%"
android:endColor="#060d96"
android:startColor="#0e7e1d"
android:type="linear" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
<size
android:width="270dp"
android:height="60dp" />
<stroke
android:width="3dp"
android:color="#000000" />
이 링크에는 당신이 필요로 하는 모든 정보가 있습니다.여기서
Shape.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#EAEAEA"/>
<corners android:bottomLeftRadius="8dip"
android:topRightRadius="8dip"
android:topLeftRadius="1dip"
android:bottomRightRadius="1dip"
/>
</shape>
및 main.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello Android from NetBeans"/>
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nishant Nair"
android:padding="5dip"
android:layout_gravity="center"
android:background="@drawable/button_shape"
/>
</LinearLayout>
이렇게 하면 원하는 결과를 얻을 수 있습니다.
행운을 빌어요.
코너 반경을 변경하고 버튼을 눌렀을 때 리플 효과를 원한다면 다음을 사용합니다.
- button_background.xml을 그리기 가능한 위치에 놓습니다.
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#F7941D">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#F7941D" />
<corners android:radius="10dp" />
</shape>
</item>
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:radius="10dp" />
</shape>
</item>
</ripple>
- 이 배경을 단추에 적용
<Button
android:background="@drawable/button_background"
android:id="@+id/myBtn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="My Button" />
재료 설계의 새로운 방법
이 속성 코너 Radius만 있으면 됩니다.
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cornerRadius="@dimen/dimen_5dp"
android:backgroundTint="@color/colorReviewSelected"
android:text="click me"/>
구법
그리기 가능한 폴더
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFFFF"/>
<corners android:radius="30dp"/>
<stroke android:width="2dp" android:color="#999999"/>
</shape>
레이아웃 폴더
<Button
android:id="@+id/button2"
<!-- add style to avoid square background -->
style="@style/Widget.AppCompat.Button.Borderless"
android:background="@drawable/corner_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
정사각형 배경을 피하려면 스타일을 추가해야 합니다.
있습니다.app:cornerRadius
정규 분포의 속성Button
꼬리표를 달다
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#009688"
android:onClick="xyz"
android:paddingHorizontal="64dp"
android:text="@string/login"
app:cornerRadius="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/passwordCustom"
app:layout_constraintVertical_bias="0.1"
/>
벡터 드로잉 가능한 변수를 사용하는 경우 드로잉 가능한 정의에 <변수> 요소를 지정하기만 하면 됩니다.저는 이것을 블로그 게시물에서 다루었습니다.
비트맵 / 9-패치 드로잉을 사용하는 경우 비트맵 이미지에 투명도가 있는 모서리를 만들어야 합니다.
아래와 같이 카드 레이아웃을 사용할 수도 있습니다.
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="60dp"
app:cardCornerRadius="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Template"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
이것은 간단한 CardView입니다.단추의 모서리를 둥글게 만들 수 있습니다.카드의 모서리 반지름을 설정하고 표고와 같은 카드의 다른 기능을 사용할 수 있습니다.
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="30dp"
android:background="#fff"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp">
<Button
android:id="@+id/button_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="20sp"
app:cornerRadius="32dp"/>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/add_coment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="15dp"
android:clickable="true"
android:foreground="?selectableItemBackground"
android:outlineAmbientShadowColor="@color/blue_shadow_outline"
app:cardCornerRadius="25dp"
app:layout_constraintBottom_toBottomOf="@+id/coment"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/coment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="center_horizontal"
android:background="@drawable/btn_style2"
android:fontFamily="@font/ar1"
android:paddingHorizontal="10dp"
android:text="text "
android:textColor="@color/text_color"
android:textSize="17dp" />
</androidx.cardview.widget.CardView>
그리기 가능한 XML 파일을 만들고 버튼 배경을 이 파일로 설정합니다.
XML 파일 코드의 예:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--|^@^|_[ Shadows ]_|^@^|-->
<item>
<shape>
<padding android:top="2dp" android:right="2dp" android:bottom="2dp" android:left="2dp" />
<gradient android:angle="315" android:startColor="#c2c2c2" android:endColor="#c0c0c0"/>
<corners android:radius="3dp" />
</shape>
</item>
<!--|^@^|_[ Background ]_|^@^|-->
<item>
<shape>
<gradient android:angle="135" android:startColor="#f7f7f7" android:endColor="#fbfcfc"/>
<corners android:radius="3dp" />
</shape>
</item>
</layer-list>
Android Studio에는 10dp와 같은 입력을 사용할 수 있는 CornerRadius 속성이 있습니다.
언급URL : https://stackoverflow.com/questions/6054562/how-to-make-the-corners-of-a-button-round
'programing' 카테고리의 다른 글
data.frame에 전체 또는 일부 NA(누락 값)가 있는 행을 제거합니다. (0) | 2023.06.06 |
---|---|
픽셀을 dp로 변환 (0) | 2023.06.01 |
Ruby에서 DateTime 및 Time으로 변환 (0) | 2023.06.01 |
Xcode 10 - 이미지 리터럴을 더 이상 사용할 수 없습니다. (0) | 2023.06.01 |
헤로쿠: 다른 지역 깃 지점을 헤로쿠/마스터에게 푸시하는 방법 (0) | 2023.06.01 |