zoukankan      html  css  js  c++  java
  • android 布局之RelativeLayout(相对布局)

    android 布局分为LinearLayout TableLayout RelativeLayout FreamLayout AbsoluteLayout.

     常用的有LinearLayout,TableLayout,RelativeLayout ,这几个布局不会应该手机屏幕大小而有变化。通常我们使用HVGA 大小的屏幕(320*480).

    接下来我们学习RelativeLayout.

    原文引入找不到了。这里记录一下。

    看一下效果图吧。


    还有两个常用组件的图片。



    main.xml代码如果

    <?xml version="1.0" encoding="utf-8"?>  
    <!-- 相对布局  一个控件相对于另一个控件或者容器的位置。 -->  
    <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"  
            />  
         <!-- 这个TextView相对于上一个TextView 在 它的下方所以设置属性为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"  
            >  
            <!--第一个RadioButton -->  
            <RadioButton  
              android:id="@+id/male_radiobutton"  
              android:layout_width="wrap_content"  
              android:layout_height="wrap_content"  
              android:text="男" 
              android:checked="true" 
            />  
            <!--第二个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"  
         />  
         <!-- 复选框控件 -->  
         <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">学历:</string>
        <string name="interest">爱	好:</string>
        <string name="age">年	龄:</string>
        <string name="car">汽车</string>
        <string name="sing">唱歌</string>
        <string name="movie">电影</string>
        <string name="submit">提交</string>
        <string name="prompt">学历</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为基础的开放源码操作系统,主要使用于便携设备。目前尚未有统一中文名称,中国大陆地区较多人使用安卓(非官方)或安致(官方)。Android操作系统最初由Andy Rubin开发,最初主要支持手机。2005年由Google收购注资,并拉拢多家制造商组成开放手机联盟开发改良,逐渐扩展到到平板电脑及其他领域上。 2010年末数据显示,仅正式推出两年的操作系统的Android已经超越称霸十年的诺基亚Symbian系统,跃居全球最受欢迎的智能手机平台。Android的主要竞争对手是苹果的IOS,微软的WP7以及RIM的Blackberry OS。
        </string>
    </resources>
    

    又下主要代码的实现,有一个通知的代码。

    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;//爱好
    	 StringBuffer hobby = new StringBuffer();
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            brithdayEditText =(EditText) this.findViewById(R.id.brithday_edit);
            brithdayEditText.setOnClickListener(new View.OnClickListener() {
    			@Override
    			public void onClick(View v) {
    				Calendar c = Calendar.getInstance();
    				new DatePickerDialog(LayoutDemoActivity.this,new DatePickerDialog.OnDateSetListener()
    				{
    					@Override
    					public 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() {
    			
    			@Override
    			public 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("结果"+userName.getText() +""+ sex+""+ brithdayEditText.getText()+""+sEducation.getSelectedItem().toString()+""+hobby.toString());
    				}
    			}
    		});
    		  /**通知的demo示例*/
    		  Button notityBut = (Button)this.findViewById(R.id.notity);
    		  notityBut.setOnClickListener(new View.OnClickListener() {
    			
    			@Override
    			public 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="启动otherActivity通知";
    				notify.when=System.currentTimeMillis();
    				notify.defaults=Notification.DEFAULT_SOUND;
    				notify.defaults=Notification.DEFAULT_ALL;
    				notify.setLatestEventInfo(LayoutDemoActivity.this, "普通通知", "点击查看", contentIntent);
    				NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    				manager.notify(0, notify);
    			}
    		});
        }
    }

    注:通知是打开另一个activity, 


  • 相关阅读:
    UVALive 4329 Ping pong
    面试题——设计一个程序:运行报错Stack Overflow Error
    MongoDB 主从复制小实验
    我总结的18个非常好用的vim指令
    php性能优化
    error LNK2019: unresolved external symbol / error LNK2001: 无法解析的外部符号
    CopyU!v2 已经收录到腾讯软件管家!
    HDU 1498 50 years, 50 colors (行列匹配+最小顶点覆盖)
    汉语-词语-庸俗:百科
    汉语-成语-见笑大方:百科
  • 原文地址:https://www.cnblogs.com/java20130726/p/3218338.html
Copyright © 2011-2022 走看看