这篇文章主要介绍了Android AS创建自定义布局案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
先创建一个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++中创建类,要用的时候直接调用就行了。
下面展示如何调用
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++调用库-->
<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();//将标题栏隐藏起来
}
}
结果:
到此这篇关于Android AS创建自定义布局案例详解的文章就介绍到这了,更多相关Android AS创建自定义布局内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
织梦狗教程
本文标题为:Android AS创建自定义布局案例详解
基础教程推荐
猜你喜欢
- Android多返回栈技术 2023-04-15
- 解决Android Studio突然不显示logcat日志的问题 2023-02-04
- iOS开发教程之XLForm的基本使用方法 2023-05-01
- android studio按钮监听的5种方法实例详解 2023-01-12
- IOS应用内跳转系统设置相关界面的方法 2022-11-20
- Flutter绘图组件之CustomPaint使用详解 2023-05-12
- Android中的webview监听每次URL变化实例 2023-01-23
- IOS 播放系统提示音使用总结(AudioToolbox) 2023-03-01
- Android开发使用RecyclerView添加点击事件实例详解 2023-06-15
- Flutter手势密码的实现示例(附demo) 2023-04-11
