當前位置:首頁 » 安卓系統 » android控制項組合

android控制項組合

發布時間: 2023-06-29 00:16:45

A. 動態添加Android(安卓)控制項

動態添加Android(安卓)控制項步驟:
1、addView
添加控制項到布局容器
2、removeView
在布局容器中刪掉已有的控制項
3、使用
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// 生成一個LinearLayout,作為布局容器來動態添加3個Button
final LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);

// 生成3個Button
final Button btn1 = new Button(this);
btn1.setText("1");
btn1.setText("Button1");
final Button btn2 = new Button(this);
btn2.setText("2");
btn2.setText("Button2");
final Button btn3 = new Button(this);
btn3.setText("3");
btn3.setText("Button3");

// 動態把三個Button添加到
layout.addView(btn1);
layout.addView(btn2);
layout.addView(btn3);

// 點擊按鈕時,先把原來在布局容器layout上的刪掉,再添加上局容器layout,這樣本次添加的控制項就會排序到最後,以理解動態添加控制項的思路
btn1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
layout.removeView(btn1);
layout.addView(btn1);
}
});

// 同btn1一樣道理
btn2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
layout.removeView(btn2);
layout.addView(btn2);
}
});

// 同btn1一樣道理
btn3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
layout.removeView(btn3);
layout.addView(btn3);
}
});
setContentView(layout);
}

}

B. Android 新控制項之ConstraintLayout(約束布局)

ConstraintLayout (約束布局) 繼承於ViewGroup 允許開發者以靈活的方式定位和調整小部件的大小

ConstraintLayout 可讓開發者使用扁平視圖層次結構(無嵌套視圖組)創建復雜的大型布局。它與 RelativeLayout 相似,其中所有的視圖均根據同級視圖與父布局之間的關系進行布局,但其靈活性要高於 RelativeLayout ,並且更易於與 Android Studio 的布局編輯器配合使用。我理解為ConstraintLayout是一個更加靈活且減少嵌套的 RelativeLayout 的布局

ConstraintLayout作為支持庫提供,開發者可以在從 API 級別 9 (Gingerbread) 開始的 Android 系統上使用。

相信在面對一些復雜的UI頁面,咱們都是使用 RelativeLayout , LinearLayout 層層嵌套實現的.雖然能實現效果.但是層層嵌套層層解析載入View 無疑會耗費載入時間,耗費手機性能.這是時候ConstraintLayout(約束布局),就應運而生了,它出現的目的就是減少嵌套,優化層層嵌套狀況帶來的弊端

要在 ConstraintLayout 中定義某個視圖的位置, 您必須為該視圖添加至少一個水平約束條件和一個垂直約束條件 。每個約束條件均表示與其他視圖、父布局或隱形引導線之間連接或對齊方式。每個約束條件均定義了視圖在豎軸或者橫軸上的位置;因此每個視圖在每個軸上都必須至少有一個約束條件,但通常情況下會需要更多約束條件。

當您將視圖拖放到布局編輯器中時,即使沒有任何約束條件,它也會停留在您放置的位置。不過,這只是為了便於修改;當您在設備上運行布局時,如果視圖沒有任何約束條件,則會在位置 [0,0](左上角)處進行繪制。

在圖 1 中,布局在編輯器中看起來很完美,但視圖 C 上卻沒有垂直約束條件。在設備上繪制此布局時,雖然視圖 C 與視圖 A 的左右邊緣水平對齊,但由於沒有垂直約束條件,它會顯示在屏幕頂部

請注意,約束中不能有循環依賴。

相對定位是在 ConstraintLayout 中創建布局的基本構建塊之一。這些約束允許您相對於另一個小部件定位給定的小部件。您可以在水平和垂直軸上約束一個小部件:

如下圖,這告訴系統我們希望按鈕 B 的左側被約束到按鈕 A 的右側。這樣的位置約束意味著系統將嘗試讓兩側共享相同的位置。

這是可用約束的列表:

app:layout_constraintLeft(自身)_toLeftOf(相對於的控制項)="相對的控制項ID"

1.2 layout_constraintBaseline_toBaselineOf 基線對齊

如果設置了側邊距,它們將應用於相應的約束(如果存在)(圖 ),將邊距強制為目標端和源端之間的空間。通常的布局邊距屬性可用於此效果

2.1屬性:

請注意,邊距只能為正數或等於零,並且取Dimension.

2.2. 約束目標View.GONE的時候 的邊距

3.1 居中定位,就是把定位控制項的左邊對應目標的左邊 右邊對應目標的右邊,上邊對應目標的上邊

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"

如上圖,Button的左邊位於父布局的左邊,右邊位於父布局的右邊就做到了水平居中的效果

3.2 偏移 : 有時候居中展示還需要做出偏移效果

可以以一定角度和距離約束一個小部件中心相對於另一個小部件中心。這允許您將一個小部件定位在一個圓圈上

ConstraintLayout對標記為 的小部件進行了特定處理View.GONE。

GONE像往常一樣,小部件不會被顯示並且不是布局本身的一部分(即,如果標記為 ,它們的實際尺寸不會改變GONE)。
但就布局計算而言,GONE小部件仍然是其中的一部分,但有一個重要區別:

注意:
使用的邊距將是 B 在連接到 A 時定義的邊距(參見圖 7 示例)。在某些情況下,這可能不是您想要的邊距(例如,A 到其容器的一側有 100dp 的邊距,B 到 A 的邊距只有 16dp,將 A 標記為已消失,B 到容器的邊距為 16dp)。出於這個原因,您可以指定在連接到被標記為已消失的小部件時使用的備用邊距值(請參閱 上面有關已消失的邊距屬性的部分 )

1.1 您可以為自身定義最小和最大尺寸ConstraintLayout

1.2 控制項尺寸約束
android:layout_width可以通過 3 種不同方式設置和 android:layout_height屬性 來指定控制項的尺寸:

重要提示:
MATCH_PARENT不建議用於ConstraintLayout. 可以通過MATCH_CONSTRAINT將相應的左/右或上/下約束設置為來定義類似的行為"parent"。

WRAP_CONTENT (添加在 1 . 1中):強制約束
如果維度設置為WRAP_CONTENT,則在 1.1 之前的版本中,它們將被視為文字維度——也就是說,約束不會限制結果維度。雖然通常這已經足夠(並且更快),但在某些情況下,您可能希望使用WRAP_CONTENT,但繼續強制執行約束以限制結果維度。在這種情況下,您可以添加相應的屬性之一:

MATCH_CONSTRAINT維度(添加在 1 . 1中)
當維度設置為MATCH_CONSTRAINT時,默認行為是讓結果大小佔用所有可用空間。有幾個額外的修飾符可用:

layout_constraintWidth_min和layout_constraintHeight_min: 將設置此維度的最小尺寸
layout_constraintWidth_max和layout_constraintHeight_max: 將設置此維度的最大尺寸
layout_constraintWidth_percent和layout_constraintHeight_percent: 將此維度的大小設置為父維度的百分比

比率: 寬高比
您還可以將小部件的一個維度定義為另一個維度的比率。為此,您需要將至少一個約束維度設置為0dp(即MATCH_CONSTRAINT),並將屬性設置layout_constraintDimensionRatio為給定的比率。例如:

除此之外,在設置寬高比的值的時候,還可以在前面加W或H,分別指定寬度或高度限制。 例如:
app:layout_constraintDimensionRatio="H,2:3"指的是 高:寬=2:3
app:layout_constraintDimensionRatio="W,2:3"指的是 寬:高=2:3

...

Guildline的主要屬性:

Constraint 約束布局為了解決嵌套布局的弊端,更快的載入頁面而出現,但是約束布局需要整體架構頁面要有明確的構建頁面的思維,故而學習以及思維模式要有的.所以個人感覺是簡單頁面還是用相對布局,線性布局就夠了,對於復雜布局約束布局是你優化頁面載入的不二之選.
*寫作不容易,且贊且珍惜!!!*

C. 如何打造Android自定義的下拉列表框控制項

一、概述

Android中的有個原生的下拉列表控制項Spinner,但是這個控制項有時候不符合我們自己的要求,

比如有時候我們需要類似windows 或者web網頁中常見的那種下拉列表控制項,類似下圖這樣的:

這個時候只有自己動手寫一個了。其實實現起來不算很難,

本文實現的方案是採用TextView +ImageView+PopupWindow的組合方案。

先來看看我們的自己寫的控制項效果圖吧:(源碼在文章下面最後給出哈!)

二、自定義下拉列表框控制項的實現

1. 自定義控制項用到的布局文件和資源:

結果框的布局頁面:dropdownlist_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/compound"
android:background="@drawable/dropdown_bg_selector" >

<TextView
android:id="@+id/text"
android:layout_width="250dp"
android:layout_height="40dp"
android:paddingLeft="10dp"
android:text="文本文字"
android:gravity="center_vertical"
android:textSize="14sp"
android:padding="5dp"
android:singleLine="true" />
<ImageView
android:id="@+id/btn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_toRightOf="@+id/text"
android:src="@drawable/dropdown"
android:padding="5dp"
android:layout_centerVertical="true"
android:gravity="center"/>
</RelativeLayout>

下拉彈窗列表布局頁面:dropdownlist_popupwindow.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listView"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:divider="#666666"
android:dividerHeight="1dp"
></ListView>

</LinearLayout>

selector資源文件:

dropdown_list_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/dropdownlist_item_press"/>
<item android:drawable="@color/dropdownlist_item"/>
</selector>

dropdown_bg_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/dropdownlist_press"/>
<item android:drawable="@color/dropdownlist_bg"/>
</selector>

2. 自定義下拉列表框控制項類的實現:

我們採用了TextView+ImageView+PopupWindow的組合方案,所以我的自定義控制項需要重寫ViewGroup,由於我們已經知道了,布局方向為豎直方向,所以這里,

我直接繼承LinearLayout來寫這個控制項。具體實現代碼如下:
package com.czm.xcdropdownlistview;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

@SuppressLint("NewApi")
/**
* 下拉列表框控制項
* @author caiming
*
*/
public class XCDropDownListView extends LinearLayout{

private TextView editText;
private ImageView imageView;
private PopupWindow popupWindow = null;
private ArrayList<String> dataList = new ArrayList<String>();
private View mView;
public XCDropDownListView(Context context) {
this(context,null);
// TODO Auto-generated constructor stub
}
public XCDropDownListView(Context context, AttributeSet attrs) {
this(context, attrs,0);
// TODO Auto-generated constructor stub
}
public XCDropDownListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
initView();
}

public void initView(){
String infServie = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater layoutInflater;
layoutInflater = (LayoutInflater) getContext().getSystemService(infServie);
View view = layoutInflater.inflate(R.layout.dropdownlist_view, this,true);
editText= (TextView)findViewById(R.id.text);
imageView = (ImageView)findViewById(R.id.btn);
this.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(popupWindow == null ){
showPopWindow();
}else{
closePopWindow();
}
}
});
}
/**
* 打開下拉列表彈窗
*/
private void showPopWindow() {
// 載入popupWindow的布局文件
String infServie = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater layoutInflater;
layoutInflater = (LayoutInflater) getContext().getSystemService(infServie);
View contentView = layoutInflater.inflate(R.layout.dropdownlist_popupwindow, null,false);
ListView listView = (ListView)contentView.findViewById(R.id.listView);

listView.setAdapter(new XCDropDownListAdapter(getContext(), dataList));
popupWindow = new PopupWindow(contentView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.color.transparent));
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(this);
}
/**
* 關閉下拉列表彈窗
*/
private void closePopWindow(){
popupWindow.dismiss();
popupWindow = null;
}
/**
* 設置數據
* @param list
*/
public void setItemsData(ArrayList<String> list){
dataList = list;
editText.setText(list.get(0).toString());
}
/**
* 數據適配器
* @author caiming
*
*/
class XCDropDownListAdapter extends BaseAdapter{

Context mContext;
ArrayList<String> mData;
LayoutInflater inflater;
public XCDropDownListAdapter(Context ctx,ArrayList<String> data){
mContext = ctx;
mData = data;
inflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// 自定義視圖
ListItemView listItemView = null;
if (convertView == null) {
// 獲取list_item布局文件的視圖
convertView = inflater.inflate(R.layout.dropdown_list_item, null);

listItemView = new ListItemView();
// 獲取控制項對象
listItemView.tv = (TextView) convertView
.findViewById(R.id.tv);

listItemView.layout = (LinearLayout) convertView.findViewById(R.id.layout_container);
// 設置控制項集到convertView
convertView.setTag(listItemView);
} else {
listItemView = (ListItemView) convertView.getTag();
}

// 設置數據
listItemView.tv.setText(mData.get(position).toString());
final String text = mData.get(position).toString();
listItemView.layout.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText.setText(text);
closePopWindow();
}
});
return convertView;
}

}
private static class ListItemView{
TextView tv;
LinearLayout layout;
}

}

三、如何使用該自定義下拉列表框控制項

使用該控制項和使用普通的自帶的控制項一樣,首先需要在布局文件中引用該控制項:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.czm.xcdropdownlistview.MainActivity"
tools:ignore="MergeRootFrame" >

<com.czm.xcdropdownlistview.XCDropDownListView
android:id="@+id/drop_down_list_view"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />

</RelativeLayout>

其次,就是在代碼中使用該控制項:
package com.czm.xcdropdownlistview;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
/**
* 使用下拉列表框控制項 示例
* @author caiming
*
*/
public class MainActivity extends Activity {

XCDropDownListView dropDownListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

dropDownListView = (XCDropDownListView)findViewById(R.id.drop_down_list_view);
ArrayList<String> list = new ArrayList<String>();
for(int i = 0;i< 6;i++){
list.add("下拉列表項"+(i+1));
}
dropDownListView.setItemsData(list);

}

}

D. 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;
}
}
}

E. android中怎樣實現自定義控制項中的組合控制項

public
class
MyView
extends
View{
//
此處省略
構造方法
private
void
onDraw(Canvas
canvas){
//重寫view的onDraw方法,繪制控制項的樣式
//這里你使用canvas來繪制,你布局中使用這個控制項就是你繪制的樣子
}
//然後你可以定義很多自己的一些方法,用來修改控制項的樣式
//假如你自定義的一個
進度條
的話,就要修改進度條值,你就可以自定義方法,讓實現對象來改變進度值,記得修改後調用validate方法更新顯示。(具體函數記不太清了)
}
大概就是這樣實現的自定義控制項,自定義控制項的話優化是很重要的哦,不然性能會很差。
然後你要使用這個控制項的話,在布局中就需要這樣定義,假如這個自定義控制項類是這樣的:
xxx.xxx.MyView。
則使用時:

F. Android基礎技術及基本控制項

安卓基礎技術主要是講解一些安卓系統運行的原理,還有一些基本的組件,所有的安卓應用程序都是在這些組件構成的,基本控制項主要就是一些類似按鈕,進度條,滾動條這些圖形界面組件,還有一些是用戶自定義的組件。

G. android怎麼自定imagebiew的組合控制項

1.先定義該組合的XML文件布局

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="wrap_content"
4 android:layout_height="wrap_content"
5 android:orientation="horizontal" >
6 <LinearLayout
7 android:layout_width="wrap_content"
8 android:layout_height="wrap_content"
9 android:orientation="vertical"
10 >
11 <ImageView
12 android:id="@+id/touxiang"
13 android:layout_width="80dp"
14 android:layout_height="80dp"
15 android:maxWidth="80dp"
16 android:maxHeight="80dp"
17 >
18 </ImageView>
19 <ImageView
20 android:id="@+id/blood"
21 android:layout_width="wrap_content"
22 android:layout_height="wrap_content"
23 android:maxWidth="80dp"
24 android:maxHeight="20dp"
25 >
26 </ImageView>
27
28 </LinearLayout>
29
30 <LinearLayout
31 android:layout_width="wrap_content"
32 android:layout_height="wrap_content"
33 android:orientation="vertical"
34 android:layout_gravity="center_vertical"
35 >
36 <TextView
37 android:layout_width="wrap_content"
38 android:layout_height="wrap_content"
39 android:text="武器"
40 ></TextView>
41 <TextView
42 android:layout_width="wrap_content"
43 android:layout_height="wrap_content"
44 android:text="防具"
45 ></TextView>
46 <TextView
47 android:layout_width="wrap_content"
48 android:layout_height="wrap_content"
49 android:text="+1馬"
50 ></TextView>
51 <TextView
52 android:layout_width="wrap_content"
53 android:layout_height="wrap_content"
54 android:text="-1馬"
55 ></TextView>
56 </LinearLayout>
57
58 </LinearLayout>

2.自定義一個繼承布局的類

public class GeneralFrame extends LinearLayout {

ImageView general;
ImageView blood;
TextView wuqi;
TextView fangju;
TextView jiayima;
TextView jianyima;
public GeneralFrame(Context context) {
//super(context);
// TODO Auto-generated constructor stub
this(context,null);
}
public GeneralFrame(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
//在構造函數中將Xml中定義的布局解析出來。
LayoutInflater.from(context).inflate(R.layout.generalframe, this, true);
general=(ImageView)findViewById(R.id.touxiang);
blood=(ImageView)findViewById(R.id.blood);
blood.setImageResource(R.drawable.blood);
//wuqi=(TextView)findViewById(R.id
}

可在XML文件里調用該類
< com.layouts.uitest.GeneralFrame
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>

也可以在代碼中動態添加該類
GeneralFrame general=new GeneralFrame(this);
general.setGeneralImage(R.drawable.diaochan);
linear2.addView(general);

自定義View.
自定義類繼承View

public class MyView extends View
{

public MyView (Context c,AttributeSet set)
{
}
@Override
public void onDraw(Canvas canvas)
{

}
}

調用方法同自定義控制項一樣。
自定義View的構造方法一定要選中 public MyView (Context c,AttributeSet set),系統會回調該構造方法

熱點內容
精易編程 發布:2025-02-07 00:49:10 瀏覽:464
訪問桂綸鎂 發布:2025-02-07 00:49:00 瀏覽:937
安卓免費掃描哪個好 發布:2025-02-07 00:47:40 瀏覽:90
金蝶修改伺服器地址 發布:2025-02-07 00:41:08 瀏覽:867
安卓手機版的蒸汽平台在哪裡下載 發布:2025-02-07 00:29:04 瀏覽:454
php自學中心 發布:2025-02-07 00:28:50 瀏覽:270
手機卡在哪裡設置密碼 發布:2025-02-07 00:21:25 瀏覽:706
解壓哄睡音頻 發布:2025-02-07 00:19:56 瀏覽:158
資料庫系統第六版答案 發布:2025-02-07 00:06:10 瀏覽:971
fut加密毛發怎麼樣 發布:2025-02-07 00:06:01 瀏覽:868