Android学习笔记三

来源:互联网 发布:linux系统是什么意思 编辑:程序博客网 时间:2024/05/16 05:41

setContentView(R.layout.main); R cannot be resolved to a variable的问题

今天就这个问题,解决的方法是,在创建android项目的是那个版本号要写2,否则那个R.java就不会生成,R.java生成之后,这个问题也就解决了

在创建项目的时候需要注意的是那个window-》preferences要引入,路径选择完后要apply一下,然后在创建项目的是基本信息都填完了,在最后的版本栏里要记得写上2,最后finish

这是ActivityMain.java

package com.eoeAndroidLayout;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ActivityMain extends Activity {
 OnClickListener listener0 = null;
 OnClickListener listener1 = null;
 OnClickListener listener2 = null;
 OnClickListener listener3 = null;
 Button button0;
 Button button1;
 Button button2;
 Button button3;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listener0 = new OnClickListener() {
   public void onClick(View v) {
    Intent intent0 = new Intent(ActivityMain.this, ActivityFrameLayout.class);
    setTitle("FrameLayout");
    startActivity(intent0);
   }
  };
  listener1 = new OnClickListener() {
   public void onClick(View v) {
    Intent intent1 = new Intent(ActivityMain.this, ActivityRelativeLayout.class);
    startActivity(intent1);
   }
  };
  listener2 = new OnClickListener() {
   public void onClick(View v) {
    setTitle("这是在ActivityLayout");
    Intent intent2 = new Intent(ActivityMain.this, ActivityLayout.class);
    startActivity(intent2);

   }
  };
  listener3 = new OnClickListener() {
   public void onClick(View v) {
    setTitle("TableLayout");
    Intent intent3 = new Intent(ActivityMain.this, ActivityTableLayout.class);
    startActivity(intent3);

   }
  };
  setContentView(R.layout.main);
  button0 = (Button) findViewById(R.id.button0);
  button0.setOnClickListener(listener0);
  button1 = (Button) findViewById(R.id.button1);
  button1.setOnClickListener(listener1);
  button2 = (Button) findViewById(R.id.button2);
  button2.setOnClickListener(listener2);
  button3 = (Button) findViewById(R.id.button3);
  button3.setOnClickListener(listener3);

    }
}

这是ActivityFrameLayout.java

package com.eoeAndroidLayout;

import android.app.Activity;
import android.os.Bundle;

public class ActivityFrameLayout extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_frame_layout);
 }

}
这是ActivityRelativeLayout.java

package com.eoeAndroidLayout;

import android.app.Activity;
import android.os.Bundle;

public class ActivityRelativeLayout extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.relative_layout);
 }

}
这是ActivityLayout.java

package com.eoeAndroidLayout;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class ActivityLayout extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  LinearLayout layoutMain = new LinearLayout(this);
  layoutMain.setOrientation(LinearLayout.HORIZONTAL);
  setContentView(layoutMain);
  LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  RelativeLayout layoutLeft = (RelativeLayout) inflate.inflate(
    R.layout.left, null);
  RelativeLayout layoutRight = (RelativeLayout) inflate.inflate(
    R.layout.right, null);

  RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT,
    RelativeLayout.LayoutParams.WRAP_CONTENT);
  layoutMain.addView(layoutLeft, 100, 100);
  layoutMain.addView(layoutRight, relParam);

 }

}
这是ActivityTableLayout.java

package com.eoeAndroidLayout;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class ActivityTableLayout extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_table_layout);

 }

}
这些java文件创建的时候主要要继承Activity,finish之后选中相应的类名然后右键source-》override-》onCreate勾上,这样就可以了,接下来就是给主页面上的几个按钮注册,这个要在AndroidManifest.xml中注册,代码为:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.eoeAndroidLayout"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="2" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ActivityMain"
                  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 android:name=".ActivityLayout"
   android:label="演示混合Layout布局">
  </activity>
  <activity android:name=".ActivityRelativeLayout"
   android:label="演示RelativeLayout布局">
  </activity>
  <activity android:name=".ActivityFrameLayout"
   android:label="非洲地图">
  </activity>

  <activity android:name=".ActivityTableLayout"
   android:label="TableLayout布局">
  </activity>
    </application>
</manifest>

因为每个Activity都要对应一个xml文件所以在res/layout文件上右键new-》others-》android-》android xml file然后写上名字注意名字后面也要写上后缀名。

这是activity_frame_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/left"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <ImageView android:id="@+id/photo" android:src="@drawable/bg"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"  />
</FrameLayout>
这是activity_table_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:stretchColumns="1">
  <TableRow>
  <TextView android:text="用户名:" android:textStyle="bold"
   android:gravity="right" android:padding="3dip" />

  <EditText android:id="@+id/username" android:padding="3dip"
   android:scrollHorizontally="true" />
 </TableRow>

 <TableRow>
  <TextView android:text="登录密码:" android:textStyle="bold"
   android:gravity="right" android:padding="3dip" />

  <EditText android:id="@+id/password" android:password="true"
   android:padding="3dip" android:scrollHorizontally="true" />
 </TableRow>

 <TableRow android:gravity="right">

  <Button android:id="@+id/cancel"
   android:text="取消" />

  <Button android:id="@+id/login"
   android:text="登录" />
 </TableRow>
</TableLayout>
这是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"
    >
 <Button android:id="@+id/button0"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="非洲草原地图:FrameLayout的使用" />
 <Button android:id="@+id/button1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="个性化表单:RelativeLayout的使用" />
 <Button android:id="@+id/button2"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="LinearLayout和RelativeLayout互助使用" />
 <Button android:id="@+id/button3"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="整齐的表单:TableLayout的使用" />
</LinearLayout>
这是:relative_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TextView android:id="@+id/label" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="请输入用户名:" />

 <!--
  这个EditText放置在上边id为label的TextView的下边
 -->
 <EditText android:id="@+id/entry" android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="@android:drawable/editbox_background"
  android:layout_below="@id/label" />

 <!--
  取消按钮和容器的右边齐平,并且设置左边的边距为10dip
 -->
 <Button android:id="@+id/cancel" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:layout_below="@id/entry"
  android:layout_alignParentRight="true"
  android:layout_marginLeft="10dip" android:text="取消" />

 <!--
  确定按钮在取消按钮的左侧,并且和取消按钮的高度齐平
 -->
 <Button android:id="@+id/ok" android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_toLeftOf="@id/cancel"
  android:layout_alignTop="@id/cancel" android:text="确定" />
</RelativeLayout>
这是left.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/left"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TextView android:id="@+id/view1" android:background="@drawable/blue"
  android:layout_width="fill_parent"
  android:layout_height="50px" android:text="第一组第一项" />
  <TextView android:id="@+id/view2"
  android:background="@drawable/yellow"
  android:layout_width="fill_parent"
  android:layout_height="50px" android:layout_below="@id/view1"
  android:text="第一组第二项" />
</RelativeLayout>
这是:right.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/right"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TextView android:id="@+id/right_view1"
  android:background="@drawable/yellow" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="第二组第一项" />
  <TextView android:id="@+id/right_view2"
  android:background="@drawable/blue"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_below="@id/right_view1" android:text="第二组第二项" />
</RelativeLayout>
这里要注意的是关于颜色的定义是values中的strings.xml文件中定义的,代码为:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, ActivityLayoutTest</string>
 <string name="app_name">this is xml layout test</string>

 <drawable name="red">#7f00</drawable>
 <drawable name="blue">#770000ff</drawable>
 <drawable name="green">#7700ff00</drawable>
 <drawable name="yellow">#77ffff00</drawable>
</resources>
最后还要把bg.jpg放在drawable文件夹中

原创粉丝点击