Android基础 基本UI控件运用的小例子

实现图片的交换

AndroidMainifest.xml文件中的代码段 如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.day01_lx_call"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.day01_lx_call.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main.xml中的代码段如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/hello_world" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/timg" />

</RelativeLayout>

home_work.xml中代码段内容如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/txt1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="96dp"
        android:layout_marginTop="42dp"
        android:text="@string/text_01" />

    <ImageView
        android:id="@+id/img01"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginLeft="10dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txt1"
        android:layout_marginTop="42dp"
        android:src="@drawable/timg" />

    <ImageView
        android:id="@+id/img02"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/img01"
        android:layout_marginRight="16dp"
        android:src="@drawable/abc" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/txt1"
        android:layout_below="@+id/img01"
        android:layout_marginTop="66dp"
        android:text="@string/btn_01" />

</RelativeLayout>

activity_main.java 中的代码如下

package com.example.day01_lx_call;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    ImageView img01;
    ImageView img02;
    Button btn;
    boolean isChange;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置载入界面
        setContentView(R.layout.home_work);
        // 将布局中的控件和代码关联起来
        img01 = (ImageView) findViewById(R.id.img01);
        img02 = (ImageView) findViewById(R.id.img02);
        btn = (Button) findViewById(R.id.button1);
       //设立监听,这有两种方式可以写1、匿名内部类
       // 2、实现OnClickLister接口,再去写
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (!isChange) {
                    // 设置图片的资源
                    img01.setImageResource(R.drawable.abc);
                    img02.setImageResource(R.drawable.timg);
                } else {
                    img01.setImageResource(R.drawable.timg);
                    img02.setImageResource(R.drawable.abc);
                }
                isChange = !isChange;

            }
        });
    }
}

总结

1、显示文字所用的控件:TextView
加入内容的两种方式
(1) android:text=”文字的内容” 直接写明文字、或者引用String文件下的文字
(第一种用的很少,用第二种比较正式)
(2) android:text=”@string/jiao_huan” 需要在values 文件夹的 string.xml文件中进行设置(加入地址和内容。)

2、显示图片所用的控件:ImageView
android:src=”@drawable/timg” 引用drawable下的图片资源

每个控件必须定义宽度和高度
     android:layout_width="wrap_content"(自适应)
     android:layout_height="wrap_content"
     android:layout_width="match_parent"充满副布局(全屏)


    android:layout_below="@+id/editText1"
    android:layout_below="@id/editText1" 该按钮在某控件的下面

3、android程序启动的过程 androidMainfest.xml–>查看此节点在那个Activity中
–>启动res中所对应的Activity–>onCreate()方法–>setContentView(R.layout. );
加载布局文件

android:id=”@+id/btn1”每个控件的唯一标识

4、对this的理解
安卓检测 去找

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

系统会自动生成 一个Activity 实例 会绑定一个this。谁去调用this就指的是谁


5、EditText:输入框控件

android:ems=”10” 当前输入框能放下10个汉字

tips:好习惯:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值