art*_*ist 429 android rounded-corners android-button
我想做一个button
圆角.有没有一种简单的方法可以在Android中实现这一目标?
Md.*_*moy 648
如果你想要这样的东西
这是代码.
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>
Run Code Online (Sandbox Code Playgroud)
2.现在使用此drawable作为视图的背景.如果视图是按钮,那么这样的事情:
<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" />
Run Code Online (Sandbox Code Playgroud)
San*_*hav 331
在drawable文件夹中创建一个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>
Run Code Online (Sandbox Code Playgroud)
将此作为背景应用于您想要使角落变圆的按钮.
或者您可以为下面的每个角使用单独的半径
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
Run Code Online (Sandbox Code Playgroud)
小智 29
这很简单.创建一个类似下面的XML文件.将其设置为按钮的背景.如果您需要更多按钮曲线,请将radius属性更改为您的愿望.
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>
Run Code Online (Sandbox Code Playgroud)
将背景设置为按钮:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_background"/>
Run Code Online (Sandbox Code Playgroud)
小智 28
在drawable文件夹中创建shape.xml
像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>
Run Code Online (Sandbox Code Playgroud)
并在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"/>
Run Code Online (Sandbox Code Playgroud)
Sky*_*Sky 16
创建文件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>
Run Code Online (Sandbox Code Playgroud)
添加到您的按钮
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/myButton"/>
Run Code Online (Sandbox Code Playgroud)
小智 11
我发现的简单方法是在drawable文件夹中创建一个新的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>
Run Code Online (Sandbox Code Playgroud)
Rav*_*ana 11
在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>
Run Code Online (Sandbox Code Playgroud)
并使用this.xml文件作为按钮背景
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_btn"
android:text="Test" />
Run Code Online (Sandbox Code Playgroud)
材料设计的新方法
你所需要的只是这个属性cornerRadius
<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"/>
Run Code Online (Sandbox Code Playgroud)
老方法
可绘制文件夹
<?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>
Run Code Online (Sandbox Code Playgroud)
布局文件夹
<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"
/>
Run Code Online (Sandbox Code Playgroud)
确保添加样式以避免方形背景
<?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>
Run Code Online (Sandbox Code Playgroud)
<Button
android:background="@drawable/button_background"
android:id="@+id/myBtn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="My Button" />
Run Code Online (Sandbox Code Playgroud)
<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" />
Run Code Online (Sandbox Code Playgroud)
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" />
Run Code Online (Sandbox Code Playgroud)
此链接包含您需要的所有信息. 这里
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>
Run Code Online (Sandbox Code Playgroud)
和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>
Run Code Online (Sandbox Code Playgroud)
这应该会给你你想要的结果.
祝你好运
有没有一种简单的方法可以在Android中实现这一目标?
是的,今天有,而且非常简单。
只需MaterialButton
将Material Components库中的与app:cornerRadius
属性一起使用。
就像是:
<com.google.android.material.button.MaterialButton
android:text="BUTTON"
app:cornerRadius="8dp"
../>
Run Code Online (Sandbox Code Playgroud)
获得具有圆角的Button就足够了。
您可以使用“材质”按钮样式之一。例如:
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
.../>
Run Code Online (Sandbox Code Playgroud)
同样从版本1.1.0开始,您还可以更改按钮的形状。只需使用shapeAppearanceOverlay
按钮样式中的属性即可:
<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>
Run Code Online (Sandbox Code Playgroud)
然后使用:
<com.google.android.material.button.MaterialButton
style="@style/MyButtonStyle"
.../>
Run Code Online (Sandbox Code Playgroud)
您还可以shapeAppearanceOverlay
在xml布局中应用:
<com.google.android.material.button.MaterialButton
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MyApp.Button.Rounded"
.../>
Run Code Online (Sandbox Code Playgroud)
该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>
Run Code Online (Sandbox Code Playgroud)
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"
/>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
518795 次 |
最近记录: |