當前位置:首頁 » 安卓系統 » android自定義布局

android自定義布局

發布時間: 2023-02-22 23:01:22

㈠ Android自定義layout怎麼寫

LinearLayout自定義方法有多種:
1、自定義xml布局,然後載入布局,自定義一個View繼承LinearLayout
2、在自定義控制項中聲明它的所有子元素,然後在Layout文件中像使用LinearLayout一樣去進行布局。

第二種比較煩 ,它需要在Layout文件中定義好子元素之後,要在代碼 onFinishInflate() 進行匹配子元素。

我就說說載入布局文件的方法吧。
首先:定義好layout文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="40dip"
android:paddingTop="5dip"
android:src="@drawable/right_icon" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dip"
android:text="主題"
android:textColor="#000000" />
<LinearLayout
android:layout_width="100dp"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="12dip"
android:paddingTop="5dip"
android:src="@drawable/home_icon" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="12dip"
android:paddingTop="5dip"
android:src="@drawable/add_icon" />
</LinearLayout>

</LinearLayout>
public class MyLinearLayout extends LinearLayout {

private ImageView imageView,iv_home,iv_add;
private TextView textView;

public MyLinearLayout (Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyLinearLayout (Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.actionbar, this);
imageView=(ImageView) findViewById(R.id.imageView1);
iv_home=(ImageView) findViewById(R.id.imageView2);
iv_add=(ImageView) findViewById(R.id.imageView3);
textView=(TextView)findViewById(R.id.textView1);

}

/**
* 設置圖片資源
*/
public void setImageResource(int resId) {
imageView.setImageResource(resId);
}

/**
* 設置顯示的文字
*/
public void setTextViewText(String text) {
textView.setText(text);
}

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<cn.com.demo.view.MyLinearLayout
android:id="@+id/ll_actionbar"
android:layout_height="fill_parent<span style="font-family: Tahoma, 'Microsoft Yahei', Simsun;">" </span>
android:layout_width="wrap_content"
android:background="@drawable/bg"
/>
</LinearLayout>
接下來自定義一個MyLinearLayout繼承LinearLayout,並且載入剛剛寫好的layout文件。(比如http://www.tiecou.com)
public class MyLinearLayout extends LinearLayout {
private ImageView imageView,iv_home,iv_add;
private TextView textView;
public MyLinearLayout (Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyLinearLayout (Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.actionbar, this);
imageView=(ImageView) findViewById(R.id.imageView1);
iv_home=(ImageView) findViewById(R.id.imageView2);
iv_add=(ImageView) findViewById(R.id.imageView3);
textView=(TextView)findViewById(R.id.textView1);
}
/**
* 設置圖片資源
*/
public void setImageResource(int resId) {
imageView.setImageResource(resId);
}

/**
* 設置顯示的文字
*/
public void setTextViewText(String text) {
textView.setText(text);
}
}
最後,要的時候使用定義好的MyLinearLayout控制項。

㈡ android 自定義ListView加自己的布局

Listview使用自定義布局,則需要創建layout,並引用layout。以下為示例代碼:


  1. 創建layout文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="wrap_content">

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/online_user_list_item_textview" android:text="TextView"></TextView>

<Button

android:text="button"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

</Button>

</LinearLayout>


2.layout面含有的textview是想要展示內容的地方。那麼構建ArrayAdapter時,應該這樣寫:

ArrayAdapter<String>adapter=newArrayAdapter<String>(this,R.layout.online_user_list_item,R.id.online_user_list_item_textview);


3.ArrayAdapter並且重寫getView方法。代碼:

public class UserListAdapter extends ArrayAdapter<User> {

private int resourceId;

public UserListAdapter(Context context, int textViewResourceId, List<User> objects) {

super(context, textViewResourceId, objects);

this.resourceId = textViewResourceId;

}

@Override

public View getView(int position, View convertView, ViewGroup parent){

User user = getItem(position);

LinearLayout userListItem = new LinearLayout(getContext());

String inflater = Context.LAYOUT_INFLATER_SERVICE;

LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);

vi.inflate(resourceId, userListItem, true);

TextView tvUsername = (TextView)userListItem.findViewById(R.id.tv_user_list_username);

TextView tvAskedNum = (TextView)userListItem.findViewById(R.id.tv_user_list_askednum);

TextView tvLastMsg = (TextView)userListItem.findViewById(R.id.tv_user_list_lastmsg);

tvUsername.setText(user.getUsername());

tvAskedNum.setText(String.valueOf(user.getAskedNum()));

tvLastMsg.setText(user.getLastMsg());

return userListItem;

}

}

4.將適配器應用到ListView即可,listview.setAdapter(arrayAdapter);即可實現。

㈢ 安卓開發android studio中怎樣自定義actionbar的布局

1theme是用於application或activity的。首先打開AndroidManifest文件查看,一般application節點都有默認主題,

2接下來打開上圖中theme所在的文件。res-->values-->styles。

3打開後。可以看到,name屬性正是步驟一中theme的值。在可以看到parent屬性的值,parent是用於繼承內置樣式的。我們接下來要在該樣式的基礎上修改。

4修改action bar的背景。可以從圖中看到,都是一個引用另一個。圖中黃色高亮的部分,是為了兼容性,可以看到其實值是相同的。在這個例子中,因為theme的parent是Theme.AppCompat.Light.DarkActionBar真正起作用的是不帶『android:』前綴的語句,是為了支持低版本的兼容包。而帶前綴的語句是API 11以上支持的。

5修改布局背景。這個在layout文件中也可以改,不過在application的theme中修改可以應用於所有activity。

㈣ [Android] 自定義 Dialog 布局設置固定寬高無效

Dialog 的自定義布局的根布局的寬度是寫固定的,顯示的時候寬度和高度不是對應的固定值。

根布局外面又添加了一層 FrameLayout,設置其寬高均為 wrap_content 來包裹以前的布局。

這個時候猜測是否因為添加自定義視圖的時候,布局參數被改寫了,然後開始查看源碼,最終發現確實是這樣的。

在下面的源碼分析中,最終發現也是用了 mWindow.setContentView(mAlertDialogLayout) 將 R.layout.alert_dialog.xml 的默認布局添加到 PhoneWindow, 和Activity一樣的。

關鍵的地方看一下 setupCustomContent() 這個方法,在添加自定義視圖的時候布局參數設置為 MATCH_PARENT 了,所以我們設置固定大小是沒有作用的,要套一層父布局解決這個問題。

㈤ Android 自定義View之Layout過程

系列文章:

在上篇文章: Android 自定義View之Measure過程 ,我們分析了Measure過程,本次將會掀開承上啟下的Layout過程神秘面紗,
通過本篇文章,你將了解到:

在上篇文章的比喻里,我們說過:

該ViewGroup 重寫了onMeasure(xx)和onLayout(xx)方法:

同時,當layout 執行結束,清除PFLAG_FORCE_LAYOUT標記,該標記會影響Measure過程是否需要執行onMeasure。

該View 重寫了onMeasure(xx)和onLayout(xx)方法:

MyViewGroup里添加了MyView、Button兩個控制項,最終運行的效果如下:

可以看出,MyViewGroup 里子布局的是橫向擺放的。我們重點關注Layout過程。實際上,MyViewGroup里我們只重寫了onLayout(xx)方法,MyView也是重寫了onLayout(xx)方法。
接下來,分析View Layout過程。

與Measure過程類似,連接ViewGroup onLayout(xx)和View onLayout(xx)之間的橋梁是View layout(xx)。

可以看出,最終都調用了setFrame(xx)方法。

對於Measure過程在onMeasure(xx)里記錄了尺寸的值,而對於Layout過程則在layout(xx)里記錄了坐標值,具體來說是在setFrame(xx)里,該方法兩個重點地方:

View.onLayout(xx)是空實現
從layout(xx)和onLayout(xx)聲明可知,這兩個方法都是可以被重寫的,接下來看看ViewGroup是否重寫了它們。

ViewGroup.layout(xx)雖然重寫了layout(xx),但是僅僅做了簡單判斷,最後還是調用了View.layout(xx)。

這重寫後將onLayout變為抽象方法,也就是說繼承自ViewGroup的類必須重寫onLayout(xx)方法。
我們以FrameLayout為例,分析其onLayout(xx)做了什麼。

FrameLayout.onLayout(xx)為子布局Layout的時候,起始坐標都是以FrameLayout為基準,並沒有記錄上一個子布局佔了哪塊位置,因此子布局的擺放位置可能會重疊,這也是FrameLayout布局特性的由來。而我們之前的Demo在水平方向上記錄了上一個子布局的擺放位置,下一個擺放時只能在它之後,因此就形成了水平擺放的功能。
由此類推,我們常說的某個子布局在父布局裡的哪個位置,決定這個位置的即是ViewGroup.onLayout(xx)。

上邊我們分析了View.layout(xx)、View.onLayout(xx)、ViewGroup.layout(xx)、ViewGroup.onLayout(xx),這四者什麼關系呢?
View.layout(xx)

View.onLayout(xx)

ViewGroup.layout(xx)

ViewGroup.onLayout(xx)

View/ViewGroup 子類需要重寫哪些方法:

用圖表示:

通過上述的描述,我們發現Measure過程和Layout過程里定義的方法比較類似:

它倆的套路比較類似:measure(xx)、layout(xx)一般不需要我們重寫,measure(xx)里調用onMeasure(xx),layout(xx)為調用者設置坐標值。
若是ViewGroup:onMeasure(xx)里遍歷子布局,並測量每個子布局,最後將結果匯總,設置自己測量的尺寸;onLayout(xx)里遍歷子布局,並設置每個子布局的坐標。
若是View:onMeasure(xx)則測量自身,並存儲測量尺寸;onLayout(xx)不需要做什麼。

Measure過程雖然比Layout過程復雜,但仔細分析後就會發現其本質就是為了設置兩個成員變數:

而Layout過程雖然比較簡單,其本質是為了設置坐標值

將Measure設置的變數和Layout設置的變數聯系起來:

此外,Measure過程通過設置PFLAG_LAYOUT_REQUIRED 標記來告訴需要進行onLayout,而Layout過程通過清除 PFLAG_FORCE_LAYOUT來告訴Measure過程不需要執行onMeasure了。
這就是Layout的承上作用

我們知道View的繪制需要依靠Canvas繪制,而Canvas是有作用區域限制的。例如我們使用:

Cavas繪制的起點是哪呢?
對於硬體繪制加速來說:正是通過Layout過程中設置的RenderNode坐標。
而對於軟體繪制來說:

關於硬體繪制加速/軟體繪制 後續文章會分析。

這就是Layout的啟下作用
以上即是Measure、Layout、Draw三者的內在聯系。
當然Layout的"承上"還需要考慮margin、gravity等參數的影響。具體用法參見最開始的Demo。

getMeasuredWidth()/getMeasuredHeight 與 getWidth/getHeight區別
我們以獲取width為例,分別來看看其方法:

getMeasuredWidth():獲取測量的寬,屬於"臨時值"
getWidth():獲取View真實的寬
在Layout過程之前,getWidth() 默認為0
何時可以獲取真實的寬、高

下篇將分析Draw()過程,我們將分析"一切都是draw出來的"道理

本篇基於 Android 10.0

㈥ Android 自定義Toast布局

有時候,我們需要自定義Toast布局樣式,我們可以通過下面的方法引入一種布局,這樣就能展示出來完美的效果;

㈦ Android自定義布局ViewGroup

2.onLayout()方法中,通過view.layout(left,right,top,bottom)。
layout方法會設置該View視圖位於父視圖的坐標軸
其中left:子布局的左側到父布局左側的距離,right:子布局的右側到父布局左側的距離,top:子布局的上側到父布局上側的距離,bottom:子布局的下側到父布局上側的距離。

㈧ 怎麼自定義Android標題欄修改TitleBar的布局

Android程序默認的Activity標題欄只能顯示一段文字,而且不能改變它的布局、顏色、標題欄的高度等。如果想要在標題欄加上個圖標、button、輸入框、進度條、修改標題欄顏色等,只能使用自定義的標題欄。自定義標題欄可以通過在onCreate函數中添加以下代碼來實現,需要注意的是代碼的順序必須按照下面的樣式,否則將無效。
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.mainactivity); //Activity的布局
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar); //標題欄的布局
雖然上面這樣可以在標題欄加入一些控制項,但是仍然不能改變標題欄的高度、背景色,要想達到這個目的,只能使用theme(主題)。因此往project里先添加一個style。改變背景色修改android:windowTitleBackgroundStyle的值,改變標題欄高度則修改android:windowTitleSize的值。下面是一個示例:






接著再修改AndroidManifest.xml文件,找到要自定義標題欄的Activity,添加上android:theme值,比如:

Java代碼
android:theme值就是上面那個style.xml文件里定義的一個style的name值。

按照以上的步驟,修改標題欄布局、高度、背景色的功能就實現了。

熱點內容
美嘉演算法口訣 發布:2025-05-16 06:03:15 瀏覽:951
c程序編譯連接 發布:2025-05-16 06:02:36 瀏覽:964
腳本魔獸 發布:2025-05-16 06:01:52 瀏覽:330
文件夾python 發布:2025-05-16 06:01:43 瀏覽:627
電腦我的世界伺服器游戲幣 發布:2025-05-16 05:27:25 瀏覽:487
索尼手機為什麼不能用安卓10 發布:2025-05-16 05:18:46 瀏覽:784
蔚來es6選擇哪些配置實用 發布:2025-05-16 05:18:05 瀏覽:130
小米如何掃碼wifi密碼 發布:2025-05-16 05:13:38 瀏覽:807
樓層密碼是什麼意思 發布:2025-05-16 05:13:37 瀏覽:13
創建文件夾失敗 發布:2025-05-16 05:12:59 瀏覽:396