我正在尝试创建一个具有两个背景的按钮,我的自定义背景
android:background="@drawable/background_profile_edit_button" + ?attr/selectableItemBackground```
Run Code Online (Sandbox Code Playgroud)
完整按钮:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/fragprofile_constraint"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="@drawable/background_profile_edit_button"
android:paddingStart="32dp"
android:paddingTop="6dp"
android:paddingEnd="32dp"
android:paddingBottom="6dp"
android:text="Edit Profile"
android:textColor="@color/colorBlackFont"
android:textSize="12sp"
android:textStyle="bold"/>
Run Code Online (Sandbox Code Playgroud)
这可以很容易地解决,android:foreground="?attr/selectableItemBackground"但是需要 api 23 而我的最小值是 19。
我还尝试了另一种在 drawable 中使用 layer-list 的方法:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<solid android:color="@color/colorWhite" />
<stroke
android:width="1dp"
android:color="@color/dividerColor" />
<corners android:radius="3dp"/>
</shape>
</item>
<item
android:drawable="?attr/selectableItemBackground">
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
此图层列表在我的更高 API 设备上完美运行。但是它导致我的 API 19 设备崩溃......
android.view.InflateException: Binary XML file line #201: Error inflating class TextView
这个人也有同样的问题,也没有得到答复:
LayerList 错误 API 19:标签需要“drawable”属性或定义可绘制对象的子标签
TLDR:_________________________________________________________
在 …
我有一个带有圆角的滚动视图。ScrollView 中是一个 LinearLayout,它具有相同的可绘制圆角。这一切工作正常。我有一个带有圆边的滚动容器。我将子项添加到具有方形边缘的 LinearLayout(vertical) 中。我正在寻找一种方法,让它们在 LL 中间保持方形,但当它们接近底部或顶部时被裁剪。基本上,我希望它们不要像现在这样在圆角处渗出。
我最初的想法是在 LL 的边缘周围使用 canvas.clipPath,希望内部的子元素不会被绘制在那里。那没有成功。有任何想法吗?
我在互联网上浏览了各种解决方案,这使我们能够创建带有圆角的视图。它们中的大多数需要使用创建自定义视图,或者在每次我们需要圆角视图时在 xml 或九补丁中创建一个 drawable。
问题是,当我实现这样的视图时,我需要为每个这样的视图创建一个 drawable,即使两个视图除了背景颜色之外的所有东西都一样。这对我来说有点刺激,我听说 iOS 框架提供了一种创建圆角视图的好方法。任何帮助将非常感激。
编辑:除了圆角,视图和阴影的按压效果也是常用的样式。请让您的解决方案包含这些影响。
android android-shapedrawable material-components material-components-android
我创建了一个自定义AlertDialog使用圆角onDraw的LinearLayout下面,
public class RoundedLinearLayout extends LinearLayout {
private Paint drawPaint;
private Paint roundPaint;
private int mCornerRadius = 100;
private RectF bounds;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public RoundedLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
onInit();
}
public RoundedLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
onInit();
}
public RoundedLinearLayout(Context context) {
super(context);
onInit();
}
protected void onInit() {
drawPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
drawPaint.setColor(0xffffffff);
drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
roundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
roundPaint.setColor(0xffffffff);
setWillNotDraw(false);
}
@Override
protected void onSizeChanged(int w, int …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个完整的圆形布局,以便我在该布局中放置任何内容。该内容应该在圆圈内。这怎么可能?我尝试了以下方法。但形状并非一直是圆形的。有时它会根据空间变成椭圆形。如何始终保持布局循环,内容应该在里面?
<LinearLayout
android:id="@+id/zodiac_Layout"
android:layout_width="100dp"
android:layout_height="100dp"
android:orientation="vertical"
android:background="@drawable/circular_background">
<ImageView
android:id="@+id/zodiac_img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:adjustViewBounds="true"
android:src="@drawable/sunrise" />
<TextView
android:id="@+id/zodiac_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:text="@string/na"
android:textColor="#fff"
android:textSize="@dimen/nakshatra_text_size" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是 circular_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#50514F"/>
<size android:width="5dp"
android:height="5dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
如果我使ImageView match_parent. 它来自布局的圆形。我希望沿着布局的圆形裁剪内容。
android ×5
clipping ×1
layer-list ×1
layout ×1
material-components-android ×1
scrollview ×1
transparency ×1