Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

手機衛士實戰:設置界面的單條目布局結構

編輯:關於 Android 開發

實現設置中心的第一個條目布局

本文將一步步完善手機衛士的「功能列表」,重點實現設置中心界面中一個條目的布局結構。

一、為九宮格條目註冊點擊事件

要點擊九宮格中的條目並跳轉,需要為 GridView 註冊 OnItemClickListener:

// 註冊九宮格單個條目的點擊事件
gv_home.setOnItemClickListener(new OnItemClickListener() {
 // position 為點中列表條目的索引
 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 switch (position) {
 case 0:
 break;
 case 8:
 Intent intent = new Intent(getApplicationContext(), SettingActivity.class);
 startActivity(intent);
 break;
 default:
 break;
 }
 }
});

二、新建 SettingActivity

點擊「設置中心」條目後需要跳轉,因此新建 SettingActivity.java:

package com.wuyudong.mobilesafe.activity;
import com.wuyudong.mobilesafe.R;
import android.app.Activity;
import android.os.Bundle;

public class SettingActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_setting);
 }
}

三、編寫 activity_setting.xml 布局

先實現設置中心中的一個標準條目,包含標題、描述與右側的 CheckBox,並用一條分割線分隔:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 <RelativeLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:padding="5dp" >
 <TextView
 android:id="@+id/tv_title"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="自動更新設置"
 android:textColor="#000"
 android:textSize="18sp" />
 <TextView
 android:id="@+id/tv_des"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@id/tv_title"
 android:text="自動更新已關閉"
 android:textColor="#000"
 android:textSize="18sp" />
 <CheckBox
 android:id="@+id/cb_box"
 android:layout_alignParentRight="true"
 android:layout_centerVertical="true"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
 <View
 android:layout_below="@id/tv_des"
 android:background="#000"
 android:layout_width="match_parent"
 android:layout_height="1dp" />
 </RelativeLayout>
</LinearLayout>

至此,設置中心的第一個條目布局結構便完成了,如下圖紅色方框所示:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved