Android AS創(chuàng)建自定義布局案例詳解
先創(chuàng)建一個title.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_launcher_foreground"
>
<!--background可以放圖片,放了合適的圖片比較好看,這里我比較隨意點,沒找到資源-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title_Back"
android:layout_margin="5dp"
android:background="@drawable/ic_launcher_background"
android:text="@string/Back"
android:textColor="#fff"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/title_Text"
android:layout_weight="1"
android:gravity="center"
android:text="This is a title"
android:textColor="#F44336"
android:textSize="24sp"
tools:ignore="HardcodedText"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title_edit"
android:layout_margin="5dp"
android:background="@drawable/ic_launcher_background"
android:text="EDIT"
android:textColor="#fff"
tools:ignore="HardcodedText" />
這里是為了自定義布局,這就像C++中創(chuàng)建類,要用的時候直接調(diào)用就行了。
下面展示如何調(diào)用
activity_main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--酷似C++調(diào)用庫-->
<include layout="@layout/title"/>
</LinearLayout>
最后記得將標題行隱藏起來,這樣才能模擬iphone的標題欄
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar=getSupportActionBar();
if(actionBar!=null)
actionBar.hide();//將標題欄隱藏起來
}
}
結(jié)果:
到此這篇關(guān)于Android AS創(chuàng)建自定義布局案例詳解的文章就介紹到這了,更多相關(guān)Android AS創(chuàng)建自定義布局內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android notifyDataSetChanged() 動態(tài)更新ListView案例詳解
- 解決java.lang.NoClassDefFoundError: android.support.v4.animation.AnimatorCompatHelper問題
- AndroidStudio報錯Emulator:PANIC:Cannot find AVD system path. Please define ANDROID_SDK_ROOT(解決方案)
- Android實現(xiàn)快速滾動FastScrollView效果
- 在Android項目中使用AspectJ的詳細攻詻
- 捕獲與解析Android NativeCrash
相關(guān)文章
淺談Android性能優(yōu)化之內(nèi)存優(yōu)化
Android的內(nèi)存優(yōu)化是性能優(yōu)化中很重要的一部分,本文將詳細介紹Android性能優(yōu)化之內(nèi)存優(yōu)化。2021-06-06
RXjava網(wǎng)絡(luò)獲取圖片數(shù)據(jù)的方法
這篇文章主要為大家詳細介紹了RXjava網(wǎng)絡(luò)獲取圖片數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08
Android中判斷網(wǎng)絡(luò)連接是否可用及監(jiān)控網(wǎng)絡(luò)狀態(tài)
獲取網(wǎng)絡(luò)信息需要在AndroidManifest.xml文件中加入相應(yīng)的權(quán)限,接下來詳細介紹Android中判斷網(wǎng)絡(luò)連接是否可用及監(jiān)控網(wǎng)絡(luò)狀態(tài),感興趣的朋友可以參考下2012-12-12
Android數(shù)據(jù)加密之Base64編碼算法的簡單實現(xiàn)
下面小編就為大家?guī)硪黄狝ndroid數(shù)據(jù)加密之Base64編碼算法的簡單實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
Android項目實戰(zhàn)之仿網(wǎng)易新聞的頁面(RecyclerView )
這篇文章主要介紹了Android項目實戰(zhàn)之仿網(wǎng)易新聞的頁面,ViewPager作為RecyclerView的Header,感興趣的小伙伴們可以參考一下2016-01-01

