Android AS创建自定义布局案例详解
这篇文章主要介绍了Android AS创建自定义布局案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
先创建一个title.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | < 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:
1 2 3 4 5 6 7 8 9 10 11 | < 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的标题栏
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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创建自定义布局案例详解的文章就介绍到这了
原文链接:https://blog.csdn.net/ITelk/article/details/120135265