首页 > 移动平台 > 详细

安卓Activity布局简述

时间:2018-02-19 20:57:59      阅读:249      评论:0      收藏:0      [点我收藏+]

Activity布局简述

基于xml的布局

Main.xml(调用工程res/layout/main.xml定义的界面元素完成布局显示)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
><!--线形布局-->
    <ImageButton
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="wrap_content模式"
            android:src="@drawable/ic_launcher"
    /><!--将某图像显示在按钮上-->
    <Button
            android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="fill_parent模式"
    /><!--这个按钮显示为fill_parent模式 -->
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Hello World, MyActivity"
    />
</LinearLayout>

 

 

 技术分享图片

 

基于Activity的布局

package com.example.myapp1;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
//系统默认生成的Activity源码文件的内容大致如下

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    //表示重写这个onCreate方法(使用onCreat()创建相应的Activity,这个方法一般是必须的;)
    // Bundle保存了应用程序上次关闭时的状态,并且可以通过一个Activity 传给另一个Activity
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv=new TextView(this);
        tv.setText("你好");//设置显示文字
        tv.setTextSize(48.0f);//设置字体大小
        tv.setTextColor(Color.BLUE);//设置字体颜色
        setContentView(tv);//语句参数为实例对象名,而不是xml布局文件


           }
}

 

 

 技术分享图片

 

安卓Activity布局简述

原文:https://www.cnblogs.com/sinceForever/p/8454367.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!