我创建了一个矩形形状,以便将其用作列表项背景.我的问题是笔划不遵循视图边框但是让笔划宽度为+/-.
这是我的形状的xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadiusRatio="1"
android:shape="rectangle" >
<solid android:color="@color/deminoir" />
<stroke
android:width="4dp"
android:color="@color/deminoir" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
这是我的风格的xml:
<style name="champ">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:orientation">vertical</item>
<item name="android:background">@drawable/bordurechamp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
最后是我的列表项视图的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/champ" >
<!-- titre -->
<TextView
android:id="@+id/titre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/deminoir"
android:padding="5dip"
android:text="titre"
android:textAppearance="@android:style/TextAppearance.Large" />
<!-- Contenu -->
<TextView
android:id="@+id/valeur"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="5dip"
android:text="valeur"
android:textAppearance="@android:style/TextAppearance.Medium"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud) 我试图以编程方式更改selectable_kachel_shape的颜色.这是xml文件:
kachel_ticked_style.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:id="@+id/selectable_kachel_shape"
android:shape="rectangle" >
<stroke
android:width="5dp"
android:color="@color/headrbar_color" />
</shape>
</item>
<item>
<rotate
android:fromDegrees="45"
android:pivotX="120%"
android:pivotY="100%"
android:toDegrees="45" >
<shape android:shape="line" >
<stroke
android:width="40dp"
android:color="@color/headrbar_color" />
</shape>
</rotate>
</item>
<item
android:right="5dp"
android:top="5dp">
<bitmap
android:gravity="top|right"
android:src="@drawable/selectable_tiles_check" />
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
我在片段内调用以下代码
LayerDrawable layers = (LayerDrawable) this.getActivity().getResources().getDrawable(R.drawable.kachel_ticked_style);
GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.selectable_kachel_shape));
shape.setColor(this.getActivity().getResources().getColor(android.R.color.background_dark);
Run Code Online (Sandbox Code Playgroud)
1.为什么我在shape.setColor中设置NullPointerException ?
2.如何以编程方式更改图层内部的形状内的颜色?
任何人都可以告诉我他们是如何使用'RotateDrawable'来处理它是来自代码还是XML或两者兼而有之?关于Drawables动画的文档非常糟糕,动画似乎只适用于图像.我希望能够为所有drawables制作动画.当我试图从XML获取RotateDrawble时只会导致异常.从XML中查找RotateDrawable的正确功能是什么?
非常感谢
Kerubu
TextView设置drawable的方法之间的区别在哪里?文档很模糊.
1) setCompoundDrawables (Drawable left, Drawable top, Drawable right, Drawable bottom)
2) setCompoundDrawablesRelative (Drawable start, Drawable top, Drawable end, Drawable bottom)
3) setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)
4) setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable start, Drawable top, Drawable end, Drawable bottom)
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我使用以下代码为我的活动添加了一个进度条:
<LinearLayout
android:id="@+id/linlaHeaderProgress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
<ProgressBar
android:id="@+id/pbHeaderProgress"
android:indeterminateOnly="true"
android:keepScreenOn="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ProgressBar>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
然后我称之为:
progressbar = (LinearLayout) findViewById(R.id.linlaHeaderProgress);
progressbar.setVisibility(View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)
将显示进度条,我想更改它的颜色.默认情况下,进度条以灰色显示.这是我试图改变颜色:
我在drawables文件夹中创建了一个xml文件,并将其命名activityindicator.xml
为此xml的内容为:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/secondaryProgress">
<color android:color="#f58233" />
</item>
<item android:id="@android:id/progress">
<color android:color="#f58233" />
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
我将布局文件更改为:
<LinearLayout
android:id="@+id/linlaHeaderProgress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:progressDrawable="@drawable/activityindicator"
android:orientation="vertical"
android:visibility="gone" >
<ProgressBar
android:id="@+id/pbHeaderProgress"
android:indeterminateOnly="true"
android:keepScreenOn="true"
android:progressDrawable="@drawable/activityindicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ProgressBar>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这就是我尝试过的,但颜色并没有改变.谁能告诉我我做错了什么?
我正在使用Lollipop版本.
xml android android-linearlayout progress-bar android-drawable
我正在尝试在Android中制作可自定义的图标.我制作了矢量元素,但现在我想给它一个圆形背景,所以我试着把它放在一个圆形的形状中.像这样:
<?xml version="1.0" encoding="utf-8"?>
<!-- drawable/circle_card_icon.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000"
android:pathData="M20,2H4A2,2 0 0,0 2,4V22L6,18H20A2,2 0 0,0 22,16V4A2,2 0 0,0 20,2M6,9H18V11H6M14,14H6V12H14M18,8H6V6H18" />
</vector>
</shape>
Run Code Online (Sandbox Code Playgroud)
通过仅使用矢量我不会得到背景.
(我用这个网站生成矢量)
我想使用带有径向渐变的shape drawable作为View的背景.根据javadoc,渐变的半径可以设置为特定值(可能是像素)或百分比:
机器人:gradientRadius
渐变的半径,仅用于径向渐变.
可能是浮点值,例如"1.2".
可以是小数值,它是附加%或%p的浮点数,例如"14.5%".%后缀总是表示基本大小的百分比; 可选的%p后缀提供相对于某个父容器的大小.
如果我错了,请纠正我,但在这里使用像素值是完全没用的,因为这个渐变看起来完全不同于一个屏幕密度到另一个(测试,是的,这是真的).我试着使用%和%p值,但它们根本不起作用.
我深入研究了Android源代码,了解了如何处理gradientRadius并在GradientDrawable类中找到它:
TypedValue tv = a.peekValue(
com.android.internal.R.styleable.GradientDrawableGradient_gradientRadius);
if (tv != null) {
boolean radiusRel = tv.type == TypedValue.TYPE_FRACTION;
st.mGradientRadius = radiusRel ?
tv.getFraction(1.0f, 1.0f) : tv.getFloat();
} else ...
}
Run Code Online (Sandbox Code Playgroud)
啊哈!因此,所有添加%或%p确实将我的值除以100.(我测试了这一点,因为TypeValue.getFraction()的文档更加不清楚).65%变为0.65.有道理,但没有用处.
那么使用gradientRadius的最佳方法是什么?
PS.我使用GradientDrawable以编程方式添加了我的背景,我得到了理想的结果.我使用GradientDrawable.setGradientRadius()来获取相对于视图宽度的值,并在设备之间获得一致的渐变.
我想添加一个线性布局,具有透明背景和白色边框.问题是:就我用谷歌搜索而言,我只能实现两者中的一个.
这是我做的:
在drawable中将以下内容保存为border.xml
<?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="#FFFFFF" />
</shape>
</item>
<item android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp" >
<shape android:shape="rectangle">
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)我现有的页面布局
<LinearLayout
android:id="@+id/quiz"
android:layout_width="150dp"
android:layout_height="120dp"
android:background="#66041414" <-------- replaced it with android:background="@drawable/border"
android:orientation="vertical"
android:layout_marginLeft="5dp" >
......
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)当边界被包括在内时,我得到了不透明的背景.
我希望最终结果如下:

完全坚持下去.只想找到实现它的出路.任何建议都会很有帮助.
有没有办法通过样式或其他形式更改android中禁用按钮的颜色?
我目前有以下,
绘制/ button_default.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_default_shape"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
绘制/ button_default_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="@color/button_default_background"/>
<corners android:radius="3dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
价值观/ styles.xml
<style name="AppTheme.Button">
<item name="android:background">@drawable/button_default</item>
<item name="android:textColor">@android:color/white</item>
<item name="android:textAllCaps">false</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingBottom">10dp</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:gravity">center</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">17sp</item>
<item name="android:textAppearance">@style/CustomFontAppearance</item>
</style>
Run Code Online (Sandbox Code Playgroud) 我正在创建一个普通的Checkbox视图:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)

浅绿色(#A5D6A7)是由主要风格定义的强调色:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorAccent">@color/green_light</item>
Run Code Online (Sandbox Code Playgroud)
我已经发现我无法在运行时更改此样式:如何在代码中设置colorAccent?
我想要的是在特定的复选框上更改此颜色,而不是全局覆盖应用程序.我可以在不创建特定资产的情况下完成吗?因为用户可以在运行时更改此颜色.
谢谢!
android ×10
android-drawable ×10
shape ×2
border ×1
button ×1
checkbox ×1
colors ×1
java ×1
progress-bar ×1
shapes ×1
stroke ×1
textview ×1
transparency ×1
vector ×1
xml ×1