android 布局之RelativeLayout(相对布局)
生活随笔
收集整理的這篇文章主要介紹了
android 布局之RelativeLayout(相对布局)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
android 布局分為L(zhǎng)inearLayout TableLayout?RelativeLayout FreamLayout AbsoluteLayout.
?常用的有LinearLayout,TableLayout,RelativeLayout ,這幾個(gè)布局不會(huì)應(yīng)該手機(jī)屏幕大小而有變化。通常我們使用HVGA 大小的屏幕(320*480).
接下來(lái)我們學(xué)習(xí)RelativeLayout.
原文引入找不到了。這里記錄一下。
看一下效果圖吧。
還有兩個(gè)常用組件的圖片。
main.xml代碼如果
<?xml version="1.0" encoding="utf-8"?>
<!-- 相對(duì)布局 一個(gè)控件相對(duì)于另一個(gè)控件或者容器的位置。 -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/describe_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textColor="#556055" /> <!-- 這個(gè)TextView相對(duì)于上一個(gè)TextView 在 它的下方所以設(shè)置屬性為layout_below--> <TextView android:id="@+id/username_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:text="@string/username" android:textColor="#556055" android:layout_below="@id/describe_view" /> <EditText android:id="@+id/username_edit" android:layout_width="90dp" android:layout_height="40dp" android:layout_marginTop="4dp" android:layout_toRightOf="@id/username_view" android:layout_below="@id/describe_view" /> <TextView android:id="@+id/sex_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:text="@string/sex" android:textColor="#556055" android:layout_below="@id/describe_view" android:layout_toRightOf="@id/username_edit" /> <RadioGroup android:id="@+id/sex_radiogroup" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/sex_view" android:layout_below="@id/describe_view" > <!--第一個(gè)RadioButton --> <RadioButton android:id="@+id/male_radiobutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:checked="true" /> <!--第二個(gè)RadioButton --> <RadioButton android:id="@+id/woman_radiobutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> </RadioGroup> <TextView android:id="@+id/age_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="25dp" android:text="@string/age" android:textColor="#556055" android:layout_below="@id/username_view" /> <EditText android:id="@+id/brithday_edit" android:layout_width="90dp" android:layout_height="40dp" android:layout_marginTop="4dp" android:hint="@string/selectdate" android:textSize="13sp" android:gravity="center" android:editable="false" android:layout_toRightOf="@id/age_view" android:layout_below="@id/username_edit" /><TextView android:id="@+id/education_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="25dp" android:text="@string/education" android:textColor="#556055" android:layout_below="@id/sex_view" android:layout_toRightOf="@id/brithday_edit" /> <!-- 下拉列表控件 --> <Spinner android:id="@+id/edu_spinner" android:layout_width="108dp" android:layout_height="38dp"android:prompt="@string/prompt" android:entries="@array/entries"android:layout_below="@id/sex_radiogroup" android:layout_toRightOf="@id/education_view" /> <TextView android:id="@+id/interest_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="25dp" android:text="@string/interest" android:textColor="#556055" android:layout_below="@id/age_view" /> <!-- 復(fù)選框控件 --> <CheckBox android:id="@+id/car_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/car" android:textColor="#566156" android:layout_toRightOf="@id/interest_view" android:layout_below="@id/brithday_edit" /> <CheckBox android:id="@+id/sing_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="11dp" android:text="@string/sing" android:textColor="#566156" android:layout_toRightOf="@id/car_check" android:layout_below="@id/brithday_edit" /> <CheckBox android:id="@+id/movie_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="11dp" android:text="@string/movie" android:textColor="#566156" android:layout_toRightOf="@id/sing_check" android:layout_below="@id/brithday_edit" /> <Button android:layout_width="100dp" android:layout_height="40dp" android:text="@string/notify"android:id="@+id/notity"android:layout_marginLeft="20dp" android:layout_marginTop="15dp" android:layout_below="@+id/sing_check"/><Button android:id="@+id/submit_button" android:layout_width="100dp" android:layout_height="40dp" android:text="@string/submit" android:gravity="center" android:layout_below="@id/movie_check" android:layout_marginLeft="210dp" android:layout_marginTop="15dp" /> </RelativeLayout> string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources><string name="hello">Hello World, LayoutDemoActivity!</string><string name="app_name">LayoutDemo</string><string name="username">用 戶:</string><string name="sex">性別:</string><string name="selectdate">出生日期</string><string name="education">學(xué)歷:</string><string name="interest">愛(ài) 好:</string><string name="age">年 齡:</string><string name="car">汽車</string><string name="sing">唱歌</string><string name="movie">電影</string><string name="submit">提交</string><string name="prompt">學(xué)歷</string><string-array name="entries"><item>本科</item><item>專科</item><item>研究生</item><item>碩士</item><item>博士</item></string-array><string name="notify">通知</string><string name="content">Android是一種以Linux為基礎(chǔ)的開(kāi)放源碼操作系統(tǒng),主要使用于便攜設(shè)備。目前尚未有統(tǒng)一中文名稱,中國(guó)大陸地區(qū)較多人使用安卓(非官方)或安致(官方)。Android操作系統(tǒng)最初由Andy Rubin開(kāi)發(fā),最初主要支持手機(jī)。2005年由Google收購(gòu)注資,并拉攏多家制造商組成開(kāi)放手機(jī)聯(lián)盟開(kāi)發(fā)改良,逐漸擴(kuò)展到到平板電腦及其他領(lǐng)域上。 2010年末數(shù)據(jù)顯示,僅正式推出兩年的操作系統(tǒng)的Android已經(jīng)超越稱霸十年的諾基亞Symbian系統(tǒng),躍居全球最受歡迎的智能手機(jī)平臺(tái)。Android的主要競(jìng)爭(zhēng)對(duì)手是蘋果的IOS,微軟的WP7以及RIM的Blackberry OS。</string>
</resources>
又下主要代碼的實(shí)現(xiàn),有一個(gè)通知的代碼。
package com.hkrt.action;import java.util.Calendar;import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;public class LayoutDemoActivity extends Activity {private EditText brithdayEditText = null; EditText userName;RadioButton male;RadioButton woman;Spinner sEducation;String sex=null;CheckBox car,sing,movie;//愛(ài)好StringBuffer hobby = new StringBuffer();@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);brithdayEditText =(EditText) this.findViewById(R.id.brithday_edit);brithdayEditText.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Calendar c = Calendar.getInstance();new DatePickerDialog(LayoutDemoActivity.this,new DatePickerDialog.OnDateSetListener(){@Overridepublic void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {brithdayEditText.setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);}},c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();}});userName =(EditText)this.findViewById(R.id.username_edit);male =(RadioButton)this.findViewById(R.id.male_radiobutton);woman =(RadioButton)this.findViewById(R.id.woman_radiobutton); sEducation =(Spinner)this.findViewById(R.id.edu_spinner);car = (CheckBox)this.findViewById(R.id.car_check);sing =(CheckBox) this.findViewById(R.id.sing_check);movie = (CheckBox)this.findViewById(R.id.movie_check);Button buttonSub = (Button)this.findViewById(R.id.submit_button);buttonSub.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if(userName.getText().toString().trim().length()==0){Toast toast = Toast.makeText(getApplicationContext(), "用戶名不能為空", 0);toast.setGravity(Gravity.CENTER, 0, 0);View toastView = toast.getView();ImageView image = new ImageView(LayoutDemoActivity.this);image.setImageResource(R.drawable.icon1);LinearLayout ll = new LinearLayout(LayoutDemoActivity.this);ll.addView(image);ll.addView(toastView);toast.setView(ll);toast.show();}else{if(male!=null){sex=male.getText().toString();}else{sex=woman.getText().toString();}if(car.isChecked()){hobby.append(car.getText()).append("|");} if(sing.isChecked()){hobby.append(sing.getText()).append("|");} if(movie.isChecked()){hobby.append(movie.getText());}Toast.makeText(getApplicationContext(), userName.getText() +""+ sex+""+ brithdayEditText.getText()+""+sEducation.getSelectedItem().toString()+""+hobby.toString(), 0).show();System.out.println("結(jié)果"+userName.getText() +""+ sex+""+ brithdayEditText.getText()+""+sEducation.getSelectedItem().toString()+""+hobby.toString());}}});/**通知的demo示例*/Button notityBut = (Button)this.findViewById(R.id.notity);notityBut.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(LayoutDemoActivity.this,OtherActivity.class);PendingIntent contentIntent = PendingIntent.getActivity(LayoutDemoActivity.this, 0, intent, 0);Notification notify = new Notification();notify.icon=R.drawable.icon1;//notify.sound=Uri.parse("file:///sdcard/Crazy.mp3");notify.tickerText="啟動(dòng)otherActivity通知";notify.when=System.currentTimeMillis();notify.defaults=Notification.DEFAULT_SOUND;notify.defaults=Notification.DEFAULT_ALL;notify.setLatestEventInfo(LayoutDemoActivity.this, "普通通知", "點(diǎn)擊查看", contentIntent);NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);manager.notify(0, notify);}});}
}注:通知是打開(kāi)另一個(gè)activity,?
轉(zhuǎn)載于:https://www.cnblogs.com/java20130726/archive/2011/11/08/3218338.html
總結(jié)
以上是生活随笔為你收集整理的android 布局之RelativeLayout(相对布局)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 递归与非递归转换(栈知识应用)
- 下一篇: 心定太阳神下一句是什么啊?