文章目录
一、案例操作
1、创建安卓应用【SetBasicInformation】


2、将背景图片拷贝到drawable目录

3、字符串资源文件strings.xml

<resources>
<string name="app_name">设置基本信息</string>
<string name="set_information">设置基本信息</string>
<string name="name">姓名:</string>
<string name="input_name">请输入姓名</string>
<string name="gender">性别:</string>
<string name="male">男</string>
<string name="female">女</string>
<string name="hobby">爱好:</string>
<string name="music">音乐</string>
<string name="read">阅读</string>
<string name="food">美食</string>
<string name="ok">确定</string>
<string name="clear">清除</string>
<string name="exit">退出</string>
</resources>
4、主布局资源文件activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="30dp">
<TextView
android:id="@+id/tvSetInformation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:text="@string/set_information"
android:textColor="#0000ff"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/input_name"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tvGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gender"
android:textColor="#000000"
android:textSize="16sp" />
<RadioGroup
android:id="@+id/rgGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/male" />
<RadioButton
android:id="@+id/rbFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="@string/female" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tvHobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hobby"
android:textColor="#000000"
android:textSize="16sp" />
<CheckBox
android:id="@+id/cbMusic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/music" />
<CheckBox
android:id="@+id/cbRead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read" />
<CheckBox
android:id="@+id/cbFood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/food" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp">
<Button
android:id="@+id/btnOk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="doOK"
android:text="@string/ok" />
<Button
android:id="@+id/btnClear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="doClear"
android:text="@string/clear" />
<Button
android:id="@+id/btnExit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="doExit"
android:text="@string/exit" />
</LinearLayout>
<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:textSize="15sp" />
</LinearLayout>
该博客介绍了如何创建一个基本的安卓应用,包括设置基本信息、添加背景图片、定义字符串资源以及构建主布局。在strings.xml中定义了应用的文字资源,如应用名称、按钮文本等。在activity_main.xml中,使用LinearLayout布局管理器创建了姓名输入框、性别选择、兴趣复选框以及确认、清除和退出按钮,展示了安卓UI设计的基本步骤。
2229

被折叠的 条评论
为什么被折叠?



