Android 带阴影的安卓圆形按钮
我是android新手,我的应用程序中有一个按钮:Android 带阴影的安卓圆形按钮,android,material-design,Android,Material Design,我是android新手,我的应用程序中有一个按钮: <Button android:elevation="10dp" android:id="@+id/btnAddJob" android:layout_width="50dp" android:layout_height="50dp" android:background="@drawable/round_btn" android:drawableLeft="
<Button android:elevation="10dp"
android:id="@+id/btnAddJob"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/round_btn"
android:drawableLeft="@drawable/plus"
android:paddingLeft="9dp"
android:gravity="center"
android:layout_marginLeft="300dp" />
以及圆形(btn):
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval" android:thickness="24dp" >
<stroke android:color="@color/colorText" android:width="24dp" />
<solid android:color="@color/colorText"/>
<size android:width="150dp" android:height="150dp"/>
</shape>
</item>
问题是我希望它有阴影,这样按钮看起来比其他元素高。有人能帮忙吗?round.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="@color/lgt_button"
android:startColor="@color/drk_button" />
<corners android:radius="20dip" />
<stroke
android:width="1px"
android:color="@color/drk_button" />
</shape>
来源:对于API>=21,您可以使用
ViewOutlineProvider
public class CustomViewOutlineProvider extends ViewOutlineProvider {
int roundCorner;
public CustomViewOutlineProvider(int round) {
roundCorner = round;
}
@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), roundCorner);
}
}
用户界面
只需添加材质库: 实现'com.google.android.material:material:1.2.0-alpha03' 并使用材料按钮:
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="bold"
app:cornerRadius="20dp" //you can set radius easily
/>
为什么不使用floatingAction按钮?请参阅此处:@DanielNugent是的,我不得不使用FloatingActionButton,我不知道为什么我认为这不是我的最佳实践。请始终提及您获取片段的来源。
android:background="@drawable/buttonbordershadow"
public class CustomViewOutlineProvider extends ViewOutlineProvider {
int roundCorner;
public CustomViewOutlineProvider(int round) {
roundCorner = round;
}
@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), roundCorner);
}
}
TextView textView = (TextView) findViewById(R.id.shadow_txt);
textView.setOutlineProvider(new CustomViewOutlineProvider(30));
textView.setClipToOutline(true);
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="bold"
app:cornerRadius="20dp" //you can set radius easily
/>