iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android AS创建自定义布局案例详解
  • 763
分享到

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

2024-04-02 19:04:59 763人浏览 薄情痞子
摘要

先创建一个title.xml <LinearLayout xmlns:Android="Http://schemas.android.com/apk/res/and

先创建一个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创建自定义布局内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Android AS创建自定义布局案例详解

本文链接: https://www.lsjlt.com/news/135218.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作