在我的应用程序中,我支持手机/平板电脑的外形和单独的布局我使用选择器"布局"(对于手机),"layout-sw600dp"(对于平板电脑).
以下是详细信息:
http://android-developers.blogspot.in/2011/07/new-tools-for-managing-screen-sizes.html
Typical numbers for screen width dp are:
320: a phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
480: a tweener tablet like the Streak (480x800 mdpi).
600: a 7” tablet (600x1024).
720: a 10” tablet (720x1280, 800x1280, etc).
Run Code Online (Sandbox Code Playgroud)
我正在使用Nexus 4手机,以下是它的主要内容
Size: 4.7 inches
Resolution: 768 x 1280 pixels
DPI: 318
dp: 386
Here is the dp calculation
dp = (px * 160)/dpi
dp = (768 * 160)/318
Run Code Online (Sandbox Code Playgroud)
当我在dp计算公式中使用768 px时,dp值为386,但是当我通过1280 px时,dp值为644.
根据以下理解,我认为Nexus 4版本将从sw600dp读取布局,但事实并非如此.
由于分辨率为宽度X高度,因此旋转设备时将反转.
我认为这是使用sw选择器与3.2之前的大型xlarge扇区进行的即兴创作.
我已经制作了以下ImageView,以支持选择器为"src":
public class CheckableImageView extends ImageView implements Checkable {
private boolean mChecked;
private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked };
public CheckableImageView(final Context context, final AttributeSet attrs) {
super(context, attrs);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.com_app_CheckableImageView, 0, 0);
setChecked(a.getBoolean(R.styleable.com_app_CheckableImageView_com_app_checked, false));
a.recycle();
}
@Override
public int[] onCreateDrawableState(final int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked())
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
return drawableState;
}
@Override
public void toggle() {
setChecked(!mChecked);
}
@Override
public boolean isChecked() {
return mChecked;
} …Run Code Online (Sandbox Code Playgroud) 选项卡图标:我当前的方法是创建两个文件(ic_list_selected_24dp.xml和ic_list_unselected_24dp.xml;它们基本相同但只是android:fillColor='Color HEX CODE'不同),然后创建一个选择器(selector_tabitem_list.xml)来更改状态时的可绘制颜色改变了.
// @drawable/selector_tabitem_list.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:drawable="@drawable/ic_list_selected_24dp"
android:state_selected="true" />
<item android:drawable="@drawable/ic_list_unselected_24dp"
android:state_selected="false" />
</selector>
Run Code Online (Sandbox Code Playgroud)
它有点复制,因为两个drawable是相同的.
选择器不能用于矢量drawable.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="@drawable/selector"
android:pathData="M19,3...."
</vector>
Run Code Online (Sandbox Code Playgroud)
-
// @drawable/selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<color android:color="@color/itemSelected" />
</item>
<item android:state_selected="false">
<color android:color="@color/itemUnselected" />
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
,android:fillColor="@color/state"或者.
// @color/state
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/white" android:state_selected="true" />
<item android:color="@android:color/black" android:state_selected="false" />
</selector>
Run Code Online (Sandbox Code Playgroud)
有没有办法使用一个drawable并动态更改其颜色?使用硬十六进制代码更好?
谢谢.
android android-tabs android-selector android-tablayout android-vectordrawable
我以编程方式将状态AddList"#e3bb87"中的StateListDrawable创建为StateListDrawable,但TextView.setTextColor不采用StateListDrawable(奇怪的是它在布局中工作)而是ColorStateList.我读了这个改变statelistdrawable文本颜色的android按钮
在ColorStateList的构造函数中,它只接受int数组
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{R.attr.state_pressed},
new int[]{R.attr.state_selected},
new int[]{-R.attr.state_selected},
},
new int[]{
Color.GREEN,
Color.BLUE,
Color.RED});
Run Code Online (Sandbox Code Playgroud)
color.xml中没有定义颜色,因为我下载了这个颜色属性.我怎么定义这样的?
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{R.attr.state_pressed}
},
**getThisColor**("#e3bb87"));
Run Code Online (Sandbox Code Playgroud) 我有一个视图,我设置了一个选择器背景,应该达到触摸.确实如此,但仅限于4.x. 在2.3它只是没有对触摸作出反应.可能是什么问题呢?这是布局:
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/idee_baden"
android:scaleType="centerInside" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@drawable/background_selector" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:ellipsize="end"
android:padding="4dp"
android:singleLine="true"
android:textAppearance="@style/SmallTextBold"
android:textColor="#ffffff" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
这是background_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/blue9"
android:state_pressed="true" />
<item android:drawable="@drawable/black9" />
</selector>
Run Code Online (Sandbox Code Playgroud) 我正在尝试应用通过代码创建的 ColorStateList 作为 TextView 的 TextColor。问题是,如果我使用 xml 中定义的 ColorStateList,它可以工作,但当我通过代码创建 ColorStateList 时,它不起作用。
这是我创建 ColorStateList 的方法
int[][] states = new int[][] { new int[] { android.R.attr.state_activated } };
int[] colors = new int[] { Color.parseColor("#FFFF00") };
myList = new ColorStateList(states, colors);
Run Code Online (Sandbox Code Playgroud)
我简单地将其应用到 TextView 以这种方式
myTextView.setTextColor(myList);
Run Code Online (Sandbox Code Playgroud)
并且不起作用。使用这个xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:color="@color/yellow" />
<item android:color="@color/black" />
</selector>
Run Code Online (Sandbox Code Playgroud)
它可以在 xml 中设置文本颜色,也可以通过代码以这种方式设置
myTextView.setTextColor(myTextView.getContext().getResources().getColorStateList(R.drawable.textcolor_selector));
Run Code Online (Sandbox Code Playgroud)
我在网上搜索了解决方案,但我真的找不到导致此问题的原因,有人可以帮助我吗?
谢谢
我正在尝试创建一个使用所选主题定义的属性的选择器。
属性文件
<declare-styleable name="ThemeColors">
<attr name="buttonTextColor" format="color" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
选择器.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="@android:color/black" />
<item android:color="?attr/buttonTextColor"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
主题:
<style name="Theme.Test" parent="@style/Theme.AppCompat">
<item name="buttonTextColor">@android:color/white</item>
</style>
Run Code Online (Sandbox Code Playgroud)
部分布局文件:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test"
android:textColor="@drawable/selector" />
Run Code Online (Sandbox Code Playgroud)
显现
<application
...
android:theme="@style/Theme.Test" >
Run Code Online (Sandbox Code Playgroud)
布局文件由该setContentView方法膨胀。
现在的问题是 textcolorRED处于未按下状态(按下时为黑色)。似乎找不到buttonTextColor并使用默认值RED(?)
已经尝试将主题设置为应用程序上下文,但没有运气..
提前致谢。
selector我的应用程序中有一个项目用作列表视图行的背景颜色。关键是该行在被点击/触摸时会改变颜色。
因此,选择器使用两个可绘制对象,一个用于按下状态,一个用于常规。文件:rowbgselector.xml在文件夹中res/color:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/rowbg_shape_selected" android:state_pressed="true"/>
<item android:drawable="@drawable/rowbg_shape" />
</selector>
Run Code Online (Sandbox Code Playgroud)
引用的两个可绘制对象定义res/drawable为具有纯色的简单矩形形状:
文件rowbg_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/row_bg"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
文件rowbg_shape_selected.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="?attr/colorAccent"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
这适用于 Lollipop 设备,但在 Lollipop 之前的任何设备上都失败,并出现一个没有说明太多的错误:
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/rowbg_shape_selected.xml from drawable resource ID #0x7f02006c
我认为问题在于这是 Lollipop 中修复的错误,请参见此处:https : //code.google.com/p/android/issues/detail?id=26251
问题是我试图引用attr/colorAccent当然在我的主题中定义的。我有几个不同颜色的主题,用户可以从中选择,并且attr/colorAccent每个主题都不同。然而,似乎由于棒棒糖之前的这个错误,你不能在选择器中引用这样的属性......
我的替代选择是什么?我能想到的唯一选择是为每个主题创建一个单独的选择器 xml 文件,并添加类似的内容attr/bg_selector,然后为每个主题引用正确的选择器文件。这将花费我很长时间,更进一步,更改此选择器的任何内容将需要大量工作(如果稍后我想让颜色稍微变暗或变亮,我将不得不浏览所有这些文件......)。
没有其他选择了吗?
对于我在平板电脑上的列表视图,我试图让我选择的列表项选择在选中时保持其状态但不幸的是我看到一些奇怪的行为.出于某种原因,每当我滚动列表到所选项目不可见的点,然后滚动回到所选项目可见的点时,背景颜色意外地被重用.我相信有些东西需要放在getView方法中,但我不知道如何处理这个方法.必须采取哪些措施来防止背景颜色被重复使用?
适配器类
public class VictoriaListAdapter extends BaseAdapter {
private List<Victoria> mData;
private LayoutInflater mInflater;
public VictoriaListAdapter (List<Victoria> data, Context context) {
mData = data;
mData = new ArrayList(mData);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position).getStation();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_dualline, parent, …Run Code Online (Sandbox Code Playgroud) 颜色选择器定义如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/gray" />
</shape>
</item>
<item android:state_focused="true">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#66666666" />
</shape>
</item>
<item>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/translucent_icon_background" />
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
在 Android Studio 中,“item”被标记为红色,表示出现错误,如下图所示:

根据测试,该应用程序运行良好。我之所以这么问,是因为我担心由于该错误,在某些设备上可能会出现问题。有人能解释一下这个错误吗?更具体地说,它可以被忽略吗?
android-selector ×10
android ×9
textcolor ×2
android-tabs ×1
attr ×1
attributes ×1
java ×1
textview ×1
xml ×1