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中動態(tài)添加Panel框架的實現(xiàn)代碼
項目經(jīng)常會有這種需求,就是想動態(tài)的在某個界面上添加一個Panel。比如,有一個按鈕,點擊后會顯示下拉菜單式的界面。這種需求,就屬于動態(tài)添加一個Panel。需求多了,就要研究是否可以抽象出通用的框架代碼,以方便開發(fā),所以就有了以下內(nèi)容2013-05-05
Android編程仿Iphone拖動相片特效Gallery的簡單應(yīng)用示例
這篇文章主要介紹了Android編程仿Iphone拖動相片特效Gallery的簡單應(yīng)用,結(jié)合實例形式分析了Android圖形拖動特效的實現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
Android客戶端與服務(wù)端數(shù)據(jù)加密傳輸方案詳解
這篇文章主要為大家介紹了Android客戶端與服務(wù)端數(shù)據(jù)加密傳輸方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
Android Jetpack Compose實現(xiàn)列表吸頂效果
安卓傳統(tǒng)的Recyclerview打造懸浮頭部StickyHeader的吸頂效果,十分麻煩,而在Compose中就簡單多了。因此,本文將采用Jetpack Compose實現(xiàn)列表吸頂效果,需要的可以參考一下2022-02-02
詳解Android中OkHttp3的例子和在子線程更新UI線程的方法
本篇文章主要介紹了詳解Android中OkHttp3的例子和在子線程更新UI線程的方法 ,非常具有實用價值,需要的朋友可以參考下2017-05-05
Android Studio開發(fā)中Gradle各種常見報錯問題解決方案
這篇文章主要為大家介紹了Android Studio開發(fā)中Gradle各種常見報錯問題解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
利用Android中的TextView實現(xiàn)逐字顯示動畫
在安卓程序啟動的時候,想逐字顯示一段話,每個字都有一個從透明到不透明的漸變動畫。那如何顯示這個效果,下面一起來看看。2016-08-08
Android編程實現(xiàn)類似于圓形ProgressBar的進度條效果
這篇文章主要介紹了Android編程實現(xiàn)類似于圓形ProgressBar的進度條效果,結(jié)合實例形式分析了Android通過自定義View實現(xiàn)圓形進度條效果的操作方法,需要的朋友可以參考下2017-03-03

