當前位置:首頁 » 安卓系統 » android自定義控制項xml

android自定義控制項xml

發布時間: 2022-09-08 18:11:48

A. android自定義控制項怎麼用

一、控制項自定義屬性介紹
以下示例中代碼均在values/attrs.xml 中定義,屬性均可隨意命名。
1. reference:參考某一資源ID。
示例:
<declare-styleable name = "名稱">

<attr name = "background" format = "reference" />
<attr name = "src" format = "reference" />
</declare-styleable>

2. color:顏色值。
示例:
<declare-styleable name = "名稱">

<attr name = "textColor" format = "color" />
</declare-styleable>

3. boolean:布爾值。
示例:
<declare-styleable name = "名稱">

<attr name = "focusable" format = "boolean" />
</declare-styleable>

4. dimension:尺寸值。
示例:
<declare-styleable name = "名稱">

<attr name = "layout_width" format = "dimension" />
</declare-styleable>

5. float:浮點值。
示例:
<declare-styleable name = "名稱">

<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>

6. integer:整型值。
示例:
<declare-styleable name = "名稱">

<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
</declare-styleable>

7. string:字元串。
示例:
<declare-styleable name = "名稱">

<attr name = "text" format = "string" />
</declare-styleable>

8. fraction:百分數。
示例:

<declare-styleable name="名稱">
<attr name = "pivotX" format = "fraction" />
<attr name = "pivotY" format = "fraction" />
</declare-styleable>

9. enum:枚舉值。
示例:
<declare-styleable name="名稱">

<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>

10. flag:位或運算。
示例:
<declare-styleable name="名稱">

<attr name="windowSoftInputMode">
<flag name = "stateUnspecified" value = "0" />
<flag name = "stateUnchanged" value = "1" />
<flag name = "stateHidden" value = "2" />
<flag name = "stateAlwaysHidden" value = "3" />
</attr>
</declare-styleable>

11.多類型。
示例:

<declare-styleable name = "名稱">
<attr name = "background" format = "reference|color" />
</declare-styleable>

二、屬性的使用以及自定義控制項的實現
1、構思控制項的組成元素,思考所需自定義的屬性。
比如:我要做一個 <帶陰影的按鈕,按鈕正下方有文字說明>(類似9宮格按鈕)
新建values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="custom_view">
<attr name="custom_id" format="integer" />
<attr name="src" format="reference" />
<attr name="background" format="reference" />
<attr name="text" format="string" />
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>

以上,所定義為custom_view,custom_id為按鈕id,src為按鈕,background為陰影背景,text為按鈕說明,textColor為字體顏色,textSize為字體大小。

2、怎麼自定義控制項呢,怎麼使用這些屬性呢?話不多說請看代碼,CustomView :

package com.nanlus.custom;
import com.nanlus.custom.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomView extends FrameLayout implements OnClickListener {
private CustomListener customListener = null;
private Drawable mSrc = null, mBackground = null;
private String mText = "";
private int mTextColor = 0;
private float mTextSize = 20;
private int mCustomId = 0;
private ImageView mBackgroundView = null;
private ImageButton mButtonView = null;
private TextView mTextView = null;
private LayoutParams mParams = null;
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.custom_view);
mSrc = a.getDrawable(R.styleable.custom_view_src);
mBackground = a.getDrawable(R.styleable.custom_view_background);
mText = a.getString(R.styleable.custom_view_text);
mTextColor = a.getColor(R.styleable.custom_view_textColor,
Color.WHITE);
mTextSize = a.getDimension(R.styleable.custom_view_textSize, 20);
mCustomId = a.getInt(R.styleable.custom_view_custom_id, 0);
mTextView = new TextView(context);
mTextView.setTextSize(mTextSize);
mTextView.setTextColor(mTextColor);
mTextView.setText(mText);
mTextView.setGravity(Gravity.CENTER);
mTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mButtonView = new ImageButton(context);
mButtonView.setImageDrawable(mSrc);
mButtonView.setBackgroundDrawable(null);
mButtonView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mButtonView.setOnClickListener(this);
mBackgroundView = new ImageView(context);
mBackgroundView.setImageDrawable(mBackground);
mBackgroundView.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addView(mBackgroundView);
addView(mButtonView);
addView(mTextView);
this.setOnClickListener(this);
a.recycle();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mParams = (LayoutParams) mButtonView.getLayoutParams();
if (mParams != null) {
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
mButtonView.setLayoutParams(mParams);
}
mParams = (LayoutParams) mBackgroundView.getLayoutParams();
if (mParams != null) {
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
mBackgroundView.setLayoutParams(mParams);
}
mParams = (LayoutParams) mTextView.getLayoutParams();
if (mParams != null) {
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
mTextView.setLayoutParams(mParams);
}
}
public void setCustomListener(CustomListener l) {
customListener = l;
}
@Override
public void onClick(View v) {
if (customListener != null) {
customListener.onCuscomClick(v, mCustomId);
}
}
public interface CustomListener {
void onCuscomClick(View v, int custom_id);
}
}
代碼很簡單,就不多說,下面來看看我們的CustomView是怎麼用的,請看:

3、自定義控制項的使用
話不多說,請看代碼,main.xml:
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:nanlus="http://schemas.android.com/apk/res/com.nanlus.custom"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="horizontal" >
<com.nanlus.custom.CustomView
android:id="@+id/custom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
nanlus:background="@drawable/background"
nanlus:custom_id="1"
nanlus:src="@drawable/style_button"
nanlus:text="按鈕1" >
</com.nanlus.custom.CustomView>
</LinearLayout>
</RelativeLayout>

在這里需要解釋一下,
xmlns:nanlus="http://schemas.android.com/apk/res/com.nanlus.custom"
nanlus為在xml中的前綴,com.nanlus.custom為包名

4、在Activity中,直接上代碼
package com.nanlus.custom;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.nanlus.BaseActivity;
import com.nanlus.custom.R;
import com.nanlus.custom.CustomView.CustomListener;
public class CustomActivity extends BaseActivity implements CustomListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((CustomView) this.findViewById(R.id.custom1)).setCustomListener(this);
}
@Override
public void onCuscomClick(View v, int custom_id) {
switch (custom_id) {
case 1:
Toast.makeText(this, "hello !!!", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}

B. android開發 自定義控制項怎麼在xml文件中配置 尤其是最上面的<LinearLayout >那部分

自已控制項就是你先寫好你的控制項代碼,假如你自定義了一個MyButton,然後這個MyButton類你放在com.app包裡面,那麼你在xml配置代碼如下

<com.app.MyButton
android:layout_heigjt........... 屬性/>

希望能幫到您

C. android 自定義view 怎麼獲取xml屬性

你說的是自定義view ,也就是你自己寫了個自定義的UI控制項,在xml中你只要將該控制項寫出來即可(如Button,TexView當作控制項來寫),例如:<view(你自定義的view的類名)/>,接下來你就懂了,在裡面聲明你要的屬性

D. Android自定義控制項 怎樣設置內邊距

自定義控制項,實質是重寫某個控制項的方法和屬性,同時也繼承父控制項的方法和屬性,比如內邊距,設置自定義控制項內邊距的方法: 在xml布局文件中使用標簽: android:padding="", android:paddingLeft="", android:paddingTop="", android:bottom="" 如果想要設置外邊距,使用下面標簽: android:layout_margin="",android:layout_marginLeft,android:layout_marginRight,android:layout_marginTop,android:layout_marginBottom

E. 電腦培訓分享Android之自定義控制項

一、簡單自定義控制項MyButton



每一個控制項都是一個java類,有對應的代碼,只要你能正確的編寫java代碼,那麼電腦培訓http://www.kmbdqn.com/發現可以創造出符合你需求的控制項,即自定義控制項。


1.通過繼承的方式,創建自定義控制項


通過繼承一個現有的控制項,覆蓋其界面的呈現


通過繼承一個包含若乾子控制項的布局


通過繼承一個現有的控制項,覆蓋某個響應事件


繼承一個View來完整自定義一個心控制項


2.使你的自定義控制項繼承自某個最接近的Android控制項,必須是public


一般都會調用父類的構造方法 ,注意一般有三個構造方法


覆蓋原來控制項的方法,注意是否要再調用super中的方法


在XML中以類全名的方式引用此控制項


二、復雜自定義控制項MyLogin


需要設計包含一組控制項的自定義控制項就需要用到復雜的自定義控制項


1) 使得你的自定義控制項繼承自某個接近的布局


2) 正確的實現構造方法:構造方法中實例化目標布局,同時查找到各個子布局


3) 添加相應的響應代碼來修改屬性,使得外部能訪問布局中的子控制項


4) 在XML中以類全名的方式引用此控制項,完整的包名+類名。


F. Android之自定義控制項

一、簡單自定義控制項MyButton



每一個控制項都是一個java類,有對應的代碼,只要你能正確的編寫java代碼,那麼電腦培訓http://www.kmbdqn.cn/發現可以創造出符合你需求的控制項,即自定義控制項。


1.通過繼承的方式,創建自定義控制項


通過繼承一個現有的控制項,覆蓋其界面的呈現


通過繼承一個包含若乾子控制項的布局


通過繼承一個現有的控制項,覆蓋某個響應事件


繼承一個View來完整自定義一個心控制項


2.使你的自定義控制項繼承自某個最接近的Android控制項,必須是public


一般都會調用父類的構造方法,注意一般有三個構造方法


覆蓋原來控制項的方法,注意是否要再調用super中的方法


在XML中以類全名的方式引用此控制項


二、復雜自定義控制項MyLogin


需要設計包含一組控制項的自定義控制項就需要用到復雜的自定義控制項


1)使得你的自定義控制項繼承自某個接近的布局


2)正確的實現構造方法:構造方法中實例化目標布局,同時查找到各個子布局


3)添加相應的響應代碼來修改屬性,使得外部能訪問布局中的子控制項


4)在XML中以類全名的方式引用此控制項,完整的包名+類名。


G. 我在Android 程序的xml文件中載入的自定義ImageView控制項中如何訪問Activity中的其他控制項的句柄

用著個試試:
mTextView01 = (TextView)TalkRoundSpinView.this.findViewById(R.id.textView01);
自己想辦法傳進來一個Activity或Context
View view = View.inflate(context或Activity, R.layout.***, null);再
mTextView01 = (TextView)view.findViewById(R.id.textView01);

H. Android 自定義控制項 動態設置高度

Android動態改變View控制項大小的方法:
1、聲明控制項參數獲取對象 LayoutParams lp;
2、獲取控制項參數: lp = 控制項id.getLayoutParams();
3、設置控制項參數:如高度。 lp.height -= 10;
4:、使設置生效:控制項id.setLayoutParams(lp);
例如如要把Imageview下移200px: ImageView.setPadding( ImageView.getPaddingLeft(), ImageView.getPaddingTop()+200, ImageView.getPaddingRight(), ImageView.getPaddingBottom());

I. android中xml中有些控制項的屬性裡面有 "app:.." ,此處的app:是什麼意思和一般的android:有什麼區別

區別是:這兩個是聲明的不同的命名空間,android的是系統的,app是自定義的。


Android自定義控制項的屬性,在xml中使用自己自定義的attr的時候,其中有一步就是要自定義一個xml的命名空間後然後再給自定義屬性賦值,現在發現不知道什麼時候開始Android把這個改了,現在發現可以統一用
xmlns:app="http://schemas.android.com/apk/res-auto"
而不是原來的:
xmlns:app="http://schemas.android.com/apk/App的Package名"
還有人提到在作為lib被使用的時候,也應該用res-auto

所以說區別就是如果你http://schemas.android.com/apk/後面寫的是res/包名,那就是相關包名下的自定義屬性,而res-auto就是所有的自定義包名。

J. 如何使用自定義的XML屬性

一. 什麼是自定義XML屬性
在我們使用自定義的控制項時,很多時候都需要定義一些不同於一般的XML屬性前綴(如android:layout_width)的屬性,比如這樣 app:textColor,這些就是自定義控制項需要用到的自定義控制項屬性。
二. 自定義XML屬性有什麼用
自定義XML屬性的作用在於,在採取自定義的控制項時,很多時候,系統的一般XML屬性已經不能滿足需求,比如我們在做一個具有描邊效果的TextView時,就需要有額外定義的TextView外邊框顏色和TextView內部顏色兩種顏色。這時候,使用自定義XML屬性,用戶就可以很方便地在XML中配置額外的屬性。
三. 怎麼使用自定義XML屬性
1.定義對應的屬性

在values文件夾下新建一個attar_custom.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 自定義控制項的名稱 -->
<declare-styleable name="StrokeTextView">
<!-- 自定義的屬性名稱 和對應的單位 -->
<attr name="outerColor" format="color|reference" />
<attr name="innnerColor" format="color|reference" />
</declare-styleable>
</resources>

2.在XML中定義自定義屬性
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<span style="color:#ff0000;">xmlns:app="http://schemas.android.com/apk/res-auto"</span>
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<<span style="color:#ff0000;">com.example.demo.StrokeTextView</span>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textSize="28sp"
<span style="color:#ff0000;">app:outerColor="#000000"
app:innnerColor="#ffffff"</span>
android:layout_centerInParent="true"/>

</RelativeLayout>

注意,自定義的XML屬性必須給自定義的控制項使用。

熱點內容
sql按小時分組 發布:2024-05-05 13:26:25 瀏覽:94
張藝謀我們一家訪問人 發布:2024-05-05 12:38:05 瀏覽:111
美版安卓系統怎麼安裝 發布:2024-05-05 12:37:18 瀏覽:920
qq郵箱緩存地址 發布:2024-05-05 12:37:16 瀏覽:986
電位演算法 發布:2024-05-05 12:36:01 瀏覽:727
我的世界清風斗羅大陸伺服器地址 發布:2024-05-05 12:35:50 瀏覽:453
dell伺服器如何進入bios 發布:2024-05-05 12:34:26 瀏覽:330
在線名片製作源碼 發布:2024-05-05 12:29:27 瀏覽:447
陰陽師按鍵腳本 發布:2024-05-05 12:00:33 瀏覽:760
魔獸查腳本 發布:2024-05-05 11:54:37 瀏覽:39