androiddrawable自定义
① android 是什么drawable
一.@代表引用资源
1.引用自定义资源。格式:@[package:]type/name
android:text="@string/hello"
2.引用系统资源。格式:@android:type/name
android:textColor="@android:color/opaque_red"
注意:其实@android:type/name是@[package:]type/name 的一个子类
二.@*代表引用系统的非public资源。格式:@*android:type/name
系统资源定义分public和非public。public的声明在:
<sdk_path>\platforms\android-8\data\res\values\public.xml
@*android:type/name:可以调用系统定义的所有资源
@android:type/name:只能够调用publi属性的资源。
注意:没在public.xml中声明的资源是google不推荐使用的。
三.?代表引用主题属性
另外一种资源值允许你引用当前主题中的属性的值。这个属性值只能在style资源和XML属性中使用;它允许你通过将它们改变为当前主题提供的标准变化来改变UI元素的外观,而不是提供具体的值。例如:
android:textColor="?android:textDisabledColor"
注意,这和资源引用非常类似,除了我们使用一个"?"前缀代替了"@"。当你使用这个标记时,你就提供了属性资源的名称,它将会在主题中被查找,所以你不需要显示声明这个类型(如果声明,其形式就是?android:attr/android:textDisabledColor)。除了使用这个资源的标识符来查询主题中的值代替原始的资源,其命名语法和"@"形式一致:?[namespace:]type/name,这里类型可选。
四.@+代表在创建或引用资源 。格式:@+type/name
含义:”+”表示在R.java中名为type的内部类中添加一条记录。如"@+id/button"的含义是在R.java 文件中的id 这个静态内部类添加一条常量名为button。该常量就是该资源的标识符。如果标示符(包括系统资源)已经存在则表示引用该标示符。最常用的就是在定义资源ID中,例如:
@+id/资源ID名 新建一个资源ID
@id/资源ID名 应用现有已定义的资源ID,包括系统ID
@android:id/资源ID名 引用系统ID,其等效于@id/资源ID名
android:id="@+id/selectdlg"
android:id="@android:id/text1"
android:id="@id/button3"
② eclipse android怎么自定义底部tab菜单
实现自定义tab过程如下:
1.制作4个9patch的tab样式,可参考android默认的资源
tab_unselected.9.png tab_selected.9.pngtab_press.9.pngtab_focus.9.png
这4个资源分别代表Tab的4种状态。
2.定义Tab的selector样式(就叫它tab_indicator.xml好了),将其放入drawable文件夹下,代码如下:
xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected" />
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_focus" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_focus" />
<item android:state_pressed="true" android:drawable="@drawable/tab_press" />
selector>
3.编写indicator的布局文件(不妨也叫tab_indicator.xml),将其放入layout文件夹下,代码如下:
xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="64dip"
android:layout_weight="1"
android:layout_marginLeft="-3dip"
android:layout_marginRight="-3dip"
android:orientation="vertical"
android:background="@drawable/tab_indicator">
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/tabWidgetStyle" mce_style="?android:attr/tabWidgetStyle"
/>
4.接下来就是在TabActivity中使用我们自己编写的Tab样式了:
// 首先获取TabWidget
mTabHost = getTabHost();
LinearLayout ll = (LinearLayout)mTabHost.getChildAt(0);
TabWidget tw = (TabWidget)ll.getChildAt(0);
RelativeLayout tabIndicator1 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_indicator, tw, false);
TextView tvTab1 = (TextView)tabIndicator1.getChildAt(1);
tvTab1.setText("tab1");
mTabHot = mTabHost.newTabSpec("TAB_1")
.setIndicator(tabIndicator1)
.setContent(contentIntent);
③ android自定义view接收输入法
首先定义一下自定义属性,一种好的习惯是自定义的属性集合的名字要和使用这些属性的自定义View的类名一致,当然, 这个也不是必须的, 比如如下的属性集合, 也可以用在OtherCustomeView里。
<declare-styleable name="DrawableTextView">
<attr name="drawableHeight" format="dimension"/>
<attr name="drawableWidth" format="dimension"/>
</declare-styleable>
布局文件中定义DrawableTextView的时候, 就可以使用定义好的自定义属性了,如app:开头的属性。
<com.shwy.bestjoy.view.DrawableTextView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/model_my_customer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/defaultBlueButtonColor"
android:textSize="22sp"
android:text="@string/model_my_customer"
android:gravity="center_vertical"
android:drawableLeft="@drawable/model_my_customer_icon"
android:drawablePadding="10dip"
app:drawableHeight="45dip"
app:drawableWidth="45dip" />
在DrawableTextView.java类里读出属性值
public DrawableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DrawableTextView);
//图片的高度
mDrawableHeight = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableHeight, 0);
mDrawableWidth = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableWidth, 0);
typedArray.recycle();
}
④ android开发中怎么自定义一个复选对话框
1.首先在drawable文件夹中添加drawable文件checkbox_style.xml。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checkbox_pressed" android:state_checked="true"/>
<item android:drawable="@drawable/checkbox_normal" android:state_checked="false"/>
<item android:drawable="@drawable/checkbox_normal"/>
</selector>
2.在values文件夹下的styles.xml文件中添加CustomCheckboxTheme样式。
<style name="CustomCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/checkbox_style</item>
</style>
3.在布局文件中使用CustomCheckboxTheme样式。
<CheckBox
android:id="@+id/select_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/CustomCheckboxTheme" />
使用到的图片资源
checkbox_normal.png
checkbox_pressed.png
⑤ 在Android中自定义了adapter为什么Drawable的效果显示不出来
自定义的适配器无法显示drawable的话,就要看看是否代码有写错了。贴出具体的错误代码看看
⑥ androidstudio中drawable文件夹自定义子目录的问题
有的,只是androidstudio已经隐藏掉了,因为androidstudio自动将同名的文件集合在一起,可以切换一个视图来显示,点击最上面那个Android切换成Project即可显示出来了。
⑦ android开发中drawable文件夹下的文件是干什么用的是作为对象用,还是作为方法参数用还是
作为一个资源引用,你可以在drawable放些自定义的特效,比如圆角背景,然后在xml文件中通过android:background引用
⑧ 在Android中自定义了adapter为什么Drawable的效果显示不出来
你把代码粘贴出来,这样问题更直观,你现在这样不知道是什么情况
⑨ 怎么自定义android 下拉刷新动画
我这里要实现的一种效果是下拉刷新时播放一个帧动画
增加动画列表:
[html] view plain在CODE上查看代码片派生到我的代码片
<?xml version="1.0" encoding="utf-8"?>
<!--
根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
根标签下,通过item标签对动画中的每一个图片进行声明
android:ration 表示展示所用的该图片的时间长度
-->
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"
>
<item android:drawable="@drawable/loading1" android:ration="150"></item>
<item android:drawable="@drawable/loading2" android:ration="150"></item>
<item android:drawable="@drawable/loading3" android:ration="150"></item>
<item android:drawable="@drawable/loading4" android:ration="150"></item>
</animation-list>
修改下拉刷新布局:
/PullToRefresh/res/layout/pull_to_refresh_header_simple.xml
[java] view plain在CODE上查看代码片派生到我的代码片
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<FrameLayout
android:id="@+id/fl_inner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/header_footer_top_bottom_padding"
android:paddingLeft="@dimen/header_footer_left_right_padding"
android:paddingRight="@dimen/header_footer_left_right_padding"
android:paddingTop="@dimen/header_footer_top_bottom_padding" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<ImageView
android:id="@+id/pull_to_refresh_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/loading1"
/>
</FrameLayout>
</FrameLayout>
</merge>
增加自定义的加载布局
/PullToRefresh/src/com/handmark/pulltorefresh/library/internal/TweenAnimLoadingLayout.java
[java] view plain在CODE上查看代码片派生到我的代码片
package com.handmark.pulltorefresh.library.internal;
import com.handmark.pulltorefresh.library.R;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Orientation;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
/**
* @date 2015/1/8
* @author wuwenjie
* @desc 帧动画加载布局
*/
public class TweenAnimLoadingLayout extends LoadingLayout {
private AnimationDrawable animationDrawable;
public TweenAnimLoadingLayout(Context context, Mode mode,
Orientation scrollDirection, TypedArray attrs) {
super(context, mode, scrollDirection, attrs);
// 初始化
mHeaderImage.setImageResource(R.drawable.refresh_anim);
animationDrawable = (AnimationDrawable) mHeaderImage.getDrawable();
}
// 默认图片
@Override
protected int getDefaultDrawableResId() {
return R.drawable.loading1;
}
@Override
protected void onLoadingDrawableSet(Drawable imageDrawable) {
// NO-OP
}
@Override
protected void onPullImpl(float scaleOfLayout) {
// NO-OP
}
// 下拉以刷新
@Override
protected void pullToRefreshImpl() {
// NO-OP
}
// 正在刷新时回调
@Override
protected void refreshingImpl() {
// 播放帧动画
animationDrawable.start();
}
// 释放以刷新
@Override
protected void releaseToRefreshImpl() {
// NO-OP
}
// 重新设置
@Override
protected void resetImpl() {
mHeaderImage.setVisibility(View.VISIBLE);
mHeaderImage.clearAnimation();
}
}