这篇文章主要介绍了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创建自定义布局案例详解
猜你喜欢
- Flutter实现底部和顶部导航栏 2022-08-31
- Android MaterialButton使用实例详解(告别shape、selector) 2023-06-16
- Android studio实现动态背景页面 2023-05-23
- Android实现监听音量的变化 2023-03-30
- Android实现轮询的三种方式 2023-02-17
- 作为iOS开发,这道面试题你能答出来,说明你基础很OK! 2023-09-14
- 详解flutter engine 那些没被释放的东西 2022-12-04
- SurfaceView播放视频发送弹幕并实现滚动歌词 2023-01-02
- 最好用的ios数据恢复软件:PhoneRescue for Mac 2023-09-14
- iOS 对当前webView进行截屏的方法 2023-03-01
