Android:怎样写简单的layout文件,怎样使用控件

一直期望能写简单的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();
            }   
};

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值