我正试图在UI中工作.我正在尝试为列表条目设置stateListDrawable.我想要做的就是在按下项目时更改列表项目布局的颜色,并且在按下列表项目时我也想要更改文本的颜色.
我收到以下错误堆栈:
E/AndroidRuntime( 360): FATAL EXCEPTION: main
E/AndroidRuntime( 360): android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
E/AndroidRuntime( 360): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
E/AndroidRuntime( 360): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
E/AndroidRuntime( 360): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
E/AndroidRuntime( 360): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
E/AndroidRuntime( 360): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
E/AndroidRuntime( 360): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
E/AndroidRuntime( 360): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
Run Code Online (Sandbox Code Playgroud)
膨胀的XML如下:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/help_list_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:background="@drawable/default_list_selection">
<TextView android:id="@+id/help_list_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="@drawable/help_text_color">
</TextView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
如果我android:textColor从xml中删除属性,我可以让程序工作.有没有办法可以使用stateListDrawable来控制xml中的listitem的texColor?
stateListDrawable适用android:background于LinearLayout,但不适用于TextView的textColor属性.状态列表xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/white"
android:state_pressed="true" …Run Code Online (Sandbox Code Playgroud) 我想设置一个视图背景,它是垂直条纹的常规网格.条纹在两种颜色之间交替.(例如,在单行中,可能有6个像素的浅灰色,后面是2个深灰色像素,重复以填充宽度.)
使用Bitmap(作为资源或在代码中生成)很容易做到这一点.例如:
ShapeDrawable bg = new ShapeDrawable(new RectShape());
int[] pixels = new int[] { 0xFFCCCCCC, 0xFFCCCCCC, 0xFFCCCCCC,
0xFFCCCCCC, 0xFFCCCCCC, 0xFFCCCCCC, 0xFF999999, 0xFF999999};
Bitmap bm = Bitmap.createBitmap(pixels, 8, 1, Bitmap.Config.ARGB_8888);
Shader shader = new BitmapShader(bm,
Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
bg.getPaint().setShader(shader);
view.setBackgroundDrawable(bg);
Run Code Online (Sandbox Code Playgroud)
有没有办法严格按照XML drawables这样做,而不使用位图资源?
我正在尝试简化一些图形,要求每个密度需要9个补丁,只需使用XML drawable.这是一个相当简单的图形:

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

看起来这应该是相当简单的重新创建为XML drawable,并避免创建5个不同的9补丁图形.但是,我已经研究了图层列表drawables,插图drawables,clip drawables - 它们似乎都要求你事先知道视图的大小(例如,为了保持它的2dp,你需要设置insetRight为视图的宽度 - 2dp).我也尝试使用shape标签的size标签,但这并不是一个固定的大小,只是固定的比例(因此对于较高的视图,它将按比例缩放).
我是否忽略了一些简单的东西,或者这是我必须在布局后以编程方式检查的东西?
我有一个可绘制的形状,我想用作背景.我希望形状是透明的.但到目前为止还没有.我怎么做?这是我有的:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="#80000000" />
<stroke
android:width="1dip"
android:color="#000000" />
<corners android:radius="10dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
我期望这条线<solid android:color="#80000000" />能做到这一点.
编辑:
在清单中,我设定android:theme="@android:style/Theme.Dialog".这可能是问题的原因吗?我基本上试图将这个观点置于另一个之上.更改为00或FF或其他任何不起作用.
我已经定义了两个具有不同主要,主要暗,主要光和强调颜色的主题.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
</style>
<style name="AppTheme2" parent="AppTheme">
<item name="colorPrimary">@color/primary_2</item>
<item name="colorPrimaryDark">@color/primary_dark_2</item>
<item name="colorAccent">@color/accent_2</item>
</style>
Run Code Online (Sandbox Code Playgroud)
AppTheme默认为<application/>
Then然后我在特定活动中设置AppTheme2.
在一个可绘制的文件xml中,我使用了colors.xml中定义的相同的AppTheme原色
drawable用于两个主题的许多活动,所以当有AppTheme2我看到不同的颜色.有没有办法让drawable使用当前主题的颜色为当前活动?
为某些视图设置背景的最佳方法是什么?例如2个背景变体:
那么哪种变体会更好,九补丁或可绘制的xml资源?
performance android nine-patch xml-drawable android-drawable
我正在尝试为我的按钮创建一个具有不同状态的可绘制形状.所以我写了这个:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="@android:color/black" >
<shape android:shape="rectangle" >
<solid android:color="@color/NEGATIVE_pressed" />
<stroke
android:width="1dp"
android:color="@color/ORANGE" />
<corners android:radius="4dp" />
</shape>
</item>
<item android:state_focused="true" android:color="@android:color/black" >
<shape android:shape="rectangle" >
<solid android:color="@color/NEGATIVE_focused" />
<stroke
android:width="1dp"
android:color="@color/ORANGE" />
<corners android:radius="4dp" />
</shape>
</item>
<item android:color="@android:color/black" >
<shape android:shape="rectangle" >
<solid android:color="@color/NEGATIVE" />
<stroke
android:width="1dp"
android:color="@color/NEGATIVE" />
<corners android:radius="4dp" />
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
然后在我的按钮中我用它作为 android:background="@drawable/btn_negative_selector"
但是,我想绘制一个底部边框到那个形状,就像3 dp和不同颜色,但我无法弄清楚如何做到这一点.我试过搜索,但找不到适合选择器的东西.有什么建议吗?
我有一个我非常喜欢的渐变,但我使用XML drawable创建它:
<gradient
android:startColor="#DD181C18"
android:endColor="#809C7D5A"
android:angle="90"/>
Run Code Online (Sandbox Code Playgroud)
什么是一个简单的方法来创建一个png或类似的东西?
我真正想要的是:
一个工具可以和使用该工具的指令使用#aarrggbb格式制作渐变,因为它的输入alpha /颜色(90度角,一般角度,将是一个加号).
谢谢你的帮助.
编辑:D'哦!我完全意识到ImageMagick应该能够做到这一点.我会尽快发布我的答案代码,除非有人想要打败我并获得全部赏金!
背景: 我正在为平板电脑(风景)开发一个Android应用程序,其图像资源的分辨率为1920x1200.该分辨率适合以下屏幕尺寸和密度:
drawable-xlarge-hdpi
drawable-large-xhdpi
Run Code Online (Sandbox Code Playgroud)
问题: 如果我在这两个文件夹中包含所有重复的图像资源,则APK的最终大小将不必要地大
我的方法不成功: 我尝试使用Alias来实现此处定义的drawable:http: //developer.android.com/guide/topics/resources/providing-resources.html#AliasResources
我有我的图像资源:
res/drawable-nodpi/image_cmn.png
Run Code Online (Sandbox Code Playgroud)
和相应屏幕大小和密度文件夹中的两个别名:
res/drawable-xlarge-hdpi/image.xml
res/drawable-large-xhdpi/image.xml
Run Code Online (Sandbox Code Playgroud)
image.xml:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/image_cmn" />
Run Code Online (Sandbox Code Playgroud)
当然,当我在布局文件中使用我的图像时,我引用了别名:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/image" />
Run Code Online (Sandbox Code Playgroud)
但遗憾的是Android没有正确调整我的测试平板电脑(mdpi)的资源,结果是我有更大的图像.
我试图将原始的png移动到res/drawable甚至res/raw但是结果与res/drawable-nodpi相同.
如果我将此png移动到res/drawable-xlarge-hdpi(与xml别名相同),结果是正确的但自然不能解决我的问题因为我也必须将它们复制到res/drawable-large-xhdpi和apk大小增加.
有谁知道如何实现这一目标?
我们有什么方法可以在Drawable使用中着色TextView吗?DrawableTint仅适用于API级别23及更高级别.
目前我正在使用a Vertical Linear Layout来达到我的要求.
<LinearLayout style="@style/ChoiceIllustratorIconTextContainerStyle">
<ImageView
style="@style/ChoiceIllustratorImageStyle"
android:contentDescription="@string/cd_university"
android:src="@drawable/ic_account_balance_white_24dp" />
<TextView
style="@style/ChoiceIllustratorTextStyle"
android:text="@string/ci_text_university" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Android工作室建议我使用Compound Drawble它TextView来实现这一目标.而且我能够实现它,但我找不到Tint可绘制的方法.
<TextView
style="@style/ChoiceIllustratorTextStyle"
android:drawablePadding="4dp"
android:drawableTop="@drawable/ic_account_balance_white_24dp"
android:text="@string/ci_text_university" />
Run Code Online (Sandbox Code Playgroud) android ×10
xml-drawable ×10
drawable ×3
textview ×2
alias ×1
background ×1
gradient ×1
layer-list ×1
nine-patch ×1
performance ×1
runtime ×1
transparency ×1
xml ×1