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();
}
}