Android 学习笔记(3)——Activity初步简介

Activity是可见的,包含很多控件,比如button, textView等等。可以说Activity是用户和应用程序的接口,同时,买也可以理解为是一个容器,将不同的控件装在一起,并且按照一定的布局放置,让他们看起来更加舒服。

先说说一个Activity与AndroidManifest.xml的联系,每一个新建的Activity都要在AndroidManifest.xml文件中注册。

<activity
            android:name="com.example.helloandroid.HelloActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Activity的布局。布局文件在layout中,每一个Activity都会有对应的布局文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".HelloActivity" >

    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>
RelativeLayout表示采用相对布局,layout_height 和layout_width属性表示控件的位置。
android:text="@string/hello_world" />
这段代码是给这个textView控件添加值,但是这种在布局文件中对控件值写死的方法基本没有意义,所以一般不写,在我们编写的java代码中进行改写。


在源码中自定义相关控件值

public class HelloActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello);//要使用一个布局内的组件必须先通过setContentView填充布局,findViewById必须在setContentView之后运行 
        TextView v =(TextView) findViewById(R.id.text);
        v.setText("你好!");
    }

首先调用findViewById方法通过控件的Id来得到控件,在通过setText()方法设置内容。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值