Android AS创建自定义布局 Android AS创建自定义布局案例详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android AS创建自定义布局 Android AS创建自定义布局案例详解

Gragon_ball   2021-09-09 我要评论
想了解Android AS创建自定义布局案例详解的相关内容吗,Gragon_ball在本文为您仔细讲解Android AS创建自定义布局的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,AS创建布局,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();//将标题栏隐藏起来


    }
}

结果:

这是效果图

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们