AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hanqi.test6"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".Activity1"> </activity> <activity android:name=".MainActivity"> <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"?> <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" tools:context="com.hanqi.test6.MainActivity" android:orientation="vertical"><!--orientation改方向 vertical垂直--> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮一" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮二" android:id="@+id/button2" /> </LinearLayout>
MainActivity
package com.hanqi.test6; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取文件findViewById Button button2 = (Button)findViewById(R.id.button2); //设置setText button2.setText("新按钮名"); } }
Activity1
package com.hanqi.test6; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.view.ViewGroup; import android.widget.Button; import android.widget.LinearLayout; /** * Created by Administrator on 2016/3/22. */ public class Activity1 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //创建布局管理器 LinearLayout线性布局 LinearLayout linearLayout = new LinearLayout(this); //setOrientation 布局方向 VERTICAL垂直 linearLayout.setOrientation(LinearLayout.VERTICAL); //setLayoutParams属性 创建属性集合 linearLayout.setLayoutParams( new DrawerLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); Button button = new Button(this); //设置显示的内容 button.setText("按钮二"); //setLayoutParams属性 创建属性集合 button.setLayoutParams( new DrawerLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); //把View放到布局里去 addView添加布局 linearLayout.addView(button); button.setText("按钮三"); //setLayoutParams属性 创建属性集合 button.setLayoutParams( new DrawerLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); //把View放到布局里去 addView添加布局 linearLayout.addView(button); //关联布局管理器 setContentView关联 setContentView(linearLayout); } }