阅读 342

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

服务器评测 http://www.cncsto.com/ 

服务器测评 http://www.cncsto.com/ 

站长资源 https://www.cscnn.com/ 

小鱼创业 https://www.237fa.com/ 


文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐