0
点赞
收藏
分享

微信扫一扫

Android开发:绘制按纽

Xin_So 2022-08-04 阅读 3


1 在/app/res/layout/下建立btn.xml,内容为:

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

<TextView android:id="@+id/textView1"
android:text="show something"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>

<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/btnshape"
android:textColor="#ff0000"
android:alpha="0.5"
android:text="Click Me!">
</Button>
</RelativeLayout>


2 在/app/res/drawable/下建立btnshape.xml,内容为:


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dp"/>
<solid android:color="#00ff00"/>
<stroke android:color="#000000" android:width="2dp"/>
</shape>



3 在/app/java/com.example.z.helloworld/MainActivity中编辑代码


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
Button b1;
TextView t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setContentView(R.layout.btn);
b1 = (Button)findViewById(R.id.button1);
t1 = (TextView)findViewById(R.id.textView1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t1.setText(System.currentTimeMillis() + " Hello World!");
}
});
}
}



4 工程目录如下图所示



Android开发:绘制按纽_ide

 


5 运行结果


Android开发:绘制按纽_ide_02



 

举报

相关推荐

0 条评论