android笔记一(Button)

在activity_main.xml文件中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:id="@+id/Liear_addbtn"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
    <Button
        android:text="@string/add"
        android:textColor="@color/red"
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></Button>
 <Button
        android:text="@string/btn2"
        android:textSize="20dip"
        android:gravity="bottom"
        android:textColor="@color/black"
        android:id="@+id/Button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></Button>
 <Button
        android:text="@string/btn3"
        android:textColor="@color/black"
        android:id="@+id/Button03"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"></Button>
 <Button
        android:text="@string/btn4"
        android:textColor="@color/black"
        android:id="@+id/Button04"
        android:layout_width="150dip"
        android:layout_height="80dip"></Button>
 <Button
        android:text="@string/btn5"
        android:shadowColor="#ff000000"
        android:shadowDx="2"
        android:shadowDy="2"
        android:shadowRadius="1"
        android:textColor="@color/black"
        android:id="@+id/Button05"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></Button>
</LinearLayout>

在strings.xml文件中

<resources>

    <string name="app_name">buttonTest</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
 <string name="add">点击添加新按钮</string>
 <string name="btn2">按钮2</string>
 <string name="btn3">按钮3</string>
 <string name="btn4">按钮4</string>
 <string name="btn5">按钮5</string>
</resources>

在colors.xml文件中

<?xml version="1.0" encoding="utf-8"?>
<resources>
   
    <color name="red">#FF0000</color>
    <color name="blue">#0000FF</color>
    <color name="green">#00FF00</color>
    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>
</resources>

在MainActivity.java文件中

package com.example.buttontest;

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.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
 Button add_Button;
 LinearLayout linear_addbtn;
 int count;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        add_Button=(Button)findViewById(R.id.Button01);
        add_Button.setOnClickListener(
         new OnClickListener()
         {
          public void onClick(View v){
           linear_addbtn=(LinearLayout)findViewById(R.id.Liear_addbtn);
           String info="新添加的按钮";
           Button tempbutton=new Button(MainActivity.this);
           tempbutton.setText(info+(++count));
           linear_addbtn.addView(tempbutton);
           Toast.makeText(MainActivity.this, "成功添加按钮"+count, Toast.LENGTH_SHORT).show();
          }
         }
          );
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

原文地址:https://www.cnblogs.com/newlist/p/2686481.html