一直期望能写简单的APP,今天终于实现了
1. 为什么使用 RelativeLayout
使用LineLayout存在屏幕尺寸匹配的问题,所以选择 RelativeLayout的形式
控件间相对位置不能通过拖拽工具直接生成布局,需要结合xml文件如
android:layout_below="@+id/input"
android:layout_toRightOf="@+id/bind"
2. 怎样表示控件间相对位置
表示在input的下面 below, 在bind的右侧(toRightOf)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:shrinkColumns="0" >
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="234dp"
android:text="@string/app_name"
android:textSize="20sp"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/input"
android:layout_marginTop="27dp"
android:padding="10px"
android:text="bind"
tools:ignore="PxUsage" />
<Button
android:id="@+id/unbind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/input"
android:layout_marginLeft="215dp"
android:layout_marginTop="27dp"
android:layout_toRightOf="@+id/bind"
android:text="unbind" />
<TextView
android:id="@+id/output"
android:layout_width="match_parent"
android:layout_height="388dp"
android:layout_below="@+id/click"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="34dp"
android:layout_marginEnd="0dp"
android:layout_marginBottom="6dp"
android:textColor="@color/colorPrimary"
android:textSize="15sp"
tools:ignore="HardcodedText,SmallSp" />
</RelativeLayout>
3. 怎样使用控件
3.1 获得 EditText 输入
String input = editText.getText().toString();
3.2 显示文本到TextView
textView.setText(buffer.toString());
3.3 定义button对应的行为
button.setOnClickListener(mBindListener);
private OnClickListener mBindListener = new OnClickListener() {
public void onClick(View v) {
doBindService();
}
};