activity_main.xml 布局文件
PS:==要设置为RelativeLayout,否则 UI 所有控件都在堆积在左上角!==
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_1"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:text="click"
android:visibility="visible" />
<TextView
android:id="@+id/textView1"
android:layout_width="192dp"
android:layout_height="27dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="118dp"
tools:layout_editor_absoluteX="33dp"
tools:layout_editor_absoluteY="113dp" />
</RelativeLayout>
对应的Java代码
package com.example.dahyzhu.apk_jni_test;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView1 = null;
private Button test_btn = null;
String textString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//加载活动页面
textView1 = (TextView)findViewById(R.id.textView1);
textString = "我是 TextView";
test_btn = (Button) findViewById(R.id.btn_1); //绑定Button资源
test_btn.setOnClickListener(new MyButtonListener());//设置Button监听
}
// 实现OnClickListener接口
private class MyButtonListener implements View.OnClickListener {
@Override
public void onClick(View view) {
textView1.setText(textString);
}
}
}