这是我的XML代码工作正常,唯一的问题是它在所有四个边显示边框,而我只想在顶部和底部显示边框.我怎样才能做到这一点?
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#FFFFFF" />
<stroke
android:width="3dip"
android:color="#B1BCBE"
/>
<padding
android:bottom="0dip"
android:left="0dip"
android:right="0dip"
android:top="0dip"
/>
</shape>
Run Code Online (Sandbox Code Playgroud) 我正在尝试简化一些图形,要求每个密度需要9个补丁,只需使用XML drawable.这是一个相当简单的图形:

2段:
设置为背景图像时给出此结果:

看起来这应该是相当简单的重新创建为XML drawable,并避免创建5个不同的9补丁图形.但是,我已经研究了图层列表drawables,插图drawables,clip drawables - 它们似乎都要求你事先知道视图的大小(例如,为了保持它的2dp,你需要设置insetRight为视图的宽度 - 2dp).我也尝试使用shape标签的size标签,但这并不是一个固定的大小,只是固定的比例(因此对于较高的视图,它将按比例缩放).
我是否忽略了一些简单的东西,或者这是我必须在布局后以编程方式检查的东西?
嗨,我正在尝试drawable为我的启动画面创建一个背景,我将在主题中进行设置.但是用于保持在中心的位图drawable正在变得紧张,我无法弄清楚如何保持正常.下面是我的可绘制代码:
splash_screen_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="360"
android:centerColor="@color/colorAccentXDark"
android:endColor="@color/Black"
android:gradientRadius="500dp"
android:startColor="@color/colorAccentXDark"
android:type="radial"
android:useLevel="false" />
</shape>
</item>
<item
android:bottom="50dp"
android:top="50dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="500dp"
android:innerRadiusRatio="1"
android:shape="oval">
<gradient
android:angle="360"
android:centerColor="@color/colorAccentXDark"
android:endColor="@color/colorAccentXDark"
android:gradientRadius="400dp"
android:startColor="@color/colorAccent"
android:type="radial"
android:useLevel="false" />
</shape>
</item>
<item android:gravity="center">
<bitmap android:src="@drawable/ty_logo" />
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
这是我将此drawable设置为活动背景的代码:
<style name="TYTheme" parent="SearchActivityTheme.NoActionBar">
<item name="colorPrimaryDark">@color/colorAccentXDark</item>
<item name="android:alertDialogTheme">@style/AlertDialogTheme</item>
<item name="android:windowBackground">@drawable/splash_screen_bg</item>
</style>
Run Code Online (Sandbox Code Playgroud)
所以这里的bitmap drawable ty_logo正在png我的手机中拉长.由于没有scaleType选项,bitmapDrawable我不知道如何处理它.
我正在尝试创建一个由渐变构成的背景可绘制,该渐变填充整个视图,并且左重力的图案调整大小以垂直适合同时保持其纵横比.
这就是我目前拥有的:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/myGradient"/>
<item>
<bitmap android:src="@drawable/myPattern"
android:gravity="left" />
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
两个drawable都是VectorDrawables.
这是wrap_content高度视图的背景.
这不会调整模式的大小,而是将其垂直居中.如果我将重力设置为fill_vertical,则仅在该轴上调整大小并且宽高比会中断.
有没有办法在不使用ImageViews或其他布局的情况下在XML中实现这一点?可以以编程方式完成吗?或者我需要不同的布局?
我目前正在尝试将以下XML转换为以编程方式创建,以便我可以根据需要在整个项目中设置顶角和底角.它是一个简单的图层列表,有两个矩形; 一个在另一个之上.我想用它作为几个不同视图的背景,因此结果可以扩展.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:bottom="20dp">
<shape android:shape="rectangle" >
<size android:height="20dp" />
<solid android:color="#969595" />
<corners
android:radius = "0dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
</shape>
</item>
<item android:top="20dp">
<shape android:shape="rectangle" >
<size android:height="20dp" />
<solid android:color="#7B7979" />
<corners
android:radius = "0dp"
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp" />
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
这种方法确实有效,但我需要为每个形状分别使用一个XML,具体取决于我是想要顶部,底部,两者还是没有圆角.
我目前尝试创建相同的drawable只产生了两个矩形,一个在另一个上面.我无法弄清楚如何设置矩形的位置.无论形状的界限是什么,我都看不到明显的变化.任何建议将不胜感激.
// Usage:
setBackgroundDrawable(new DualColorStateDrawable(0, 10f));
Run Code Online (Sandbox Code Playgroud)
...
private final int topColorUnselected = Color.RED;
private final int bottomColorUnselected = Color.GREEN;
private final int topColorSelected = Color.YELLOW;
private final int bottomColorSelected = Color.BLUE;
private final …Run Code Online (Sandbox Code Playgroud) 我有一个xml图层列表drawable作为视图的背景,在某些设备上没有正确显示.
我有一个带有4.2.2的Huawei Ascend Mate,可以显示它.在华硕手机平板电脑4.1.2我只看到项目形状笔划,没有看到其他项目.
我究竟做错了什么?
<?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/yellow_color" />
</shape>
</item>
<item>
<bitmap
android:gravity="center_horizontal|bottom"
android:src="@drawable/dotted_bg"
android:tileMode="repeat" />
</item>
<item>
<shape>
<stroke
android:width="4dp"
android:color="@color/yellow_color" />
</shape>
</item>
<item>
<bitmap
android:gravity="center_horizontal|bottom"
android:src="@drawable/tls_bg" />
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud) 我的问题与这个问题有关:链接.我最近将用于Android的大部分资源从用于不同分辨率的不同png文件更改为矢量绘图.我正在使用"滑动到删除"库,可以在滑动过程中更改其视图的背景,以显示带有垃圾桶图标的Gmail风格红色背景,类似于此(具有绿色背景和支票)图标):
我曾经使用以下代码执行此操作,而我仍然使用<API 21
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="@color/accent"/>
</item>
<item android:right="@dimen/activity_horizontal_margin">
<bitmap
android:gravity="right|center_vertical"
android:src="@drawable/ic_delete_24dp"/>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
但是,API 21+需要不同的语法
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="@color/accent" />
</item>
<item android:right="@dimen/activity_horizontal_margin"
android:drawable="@drawable/ic_delete_24dp"
android:gravity="right|center_vertical"/>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
现在,API 21和API 22(Lollipop)上的图标将被拉伸以填充屏幕,而不是左/右侧的图标.有没有办法指定图标的宽度?
昨天,我已切换到Android Studio 2.1.3版.今天,我重新打开了我的一个项目,并试图添加一些图层列表drawables用于ImageView.例如这一个:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:width="80px" android:height="80px"
android:top="0px" android:left="0px"
android:right="0px" android:bottom="0px">
<shape android:shape="rectangle">
<solid android:color="@color/deep_orange"/>
</shape>
</item>
<item android:width="60px" android:height="60px"
android:top="10px" android:left="10px"
android:right="10px" android:bottom="10px">
<shape android:shape="oval">
<solid android:color="@color/light_yellow"/>
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
这是我在升级Android Studio版本之前所获得的,如果我选择23作为"在IDE中渲染布局时使用的Android版本",这就是我现在在预览中获得的内容.
但是,如果我选择23以下的任何版本,我只会得到正方形:
不幸的是,在模拟器(Lollipop 22)或设备(Jellybean 18)上运行应用程序时,我也得到了正方形.
以防这有用:
Activity延伸AppCompatActivity我认为这个问题在某种程度上关系到使用的top,left,right并bottom为上层.因为如果我只是在一个正方形的顶部放置一个圆圈,预览以及"真实的东西"将按预期的方式对所有API级别起作用.
编辑我也开始了一个全新的项目,使用相同的代码/ xml和以下(自动创建)build.gradle:
Activity延伸 …有没有办法LayerDrawable在API <23上添加图层?我是否必须LayerDrawable使用构造函数创建一个新的只添加新图层?
我试图实现这样的目标,我以这种方式在 xml 中使用了图层列表:
<item>
<shape android:shape="rectangle" >
<gradient
android:centerColor="#4DD0E1"
android:endColor="#76FF03"
android:startColor="@color/colorPrimary"
android:type="linear" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape android:shape="rectangle" >
<solid android:color="#fff" />
</shape>
</item>
Run Code Online (Sandbox Code Playgroud)
这里是布局代码,形状的边框需要与图像重叠。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="12dp"
android:background="@android:color/transparent">
<ImageView
android:layout_width="100dp"
android:layout_height="75dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginRight="12dp"
android:id="@+id/goals_image" />
<FrameLayout
android:id="@+id/goals_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:clickable="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="32dp"
android:background="#00000000"
>
<TextView
android:id="@+id/goals_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:text="Lose Weight"
android:textColor="@color/black"
android:layout_margin="40dp"
/>
</FrameLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
但是如果我将背景设置为透明,则形状背景就会变成整个渐变色。如何实现具有透明背景的渐变笔触?提前致谢。
android ×10
layer-list ×10
background ×2
drawable ×2
xml ×2
bitmap ×1
drawing ×1
scaling ×1
shape ×1
xml-drawable ×1