相对布局(RelativeLayout)

特点

  • 需要找基准点,来确定控件位置

常用属性

  • 相对于某控件外侧对齐(上对下,下对上)

    1. 左边:android:layout_toLeftOf

      SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
    2. 右边:android:layout_toRightOf

    3. 上方:android:layout_above

    4. 下方:android:layout_below

  • 相对于某控件边缘对齐(上对上)

    1. 对齐上边界:android:layout_alignTop

    2. 对齐下边界:android:layout_alignBottom

    3. 对齐左边界:android:layout_alignLeft

    4. 对齐右边界:android:layout_alignRight

  • 相对于父控件边缘对齐(上对上)

    1. 左对齐:android:layout_alighParentLeft

    2. 右对齐:android:layout_alighParentRight

    3. 顶端对齐:android:layout_alighParentTop

    4. 底部对齐:android:layout_alighParentBottom

    5. 水平居中:android:layout_centerHorizontal

    6. 垂直居中:android:layout_centerVertical

    7. 中央位置:android:layout_centerInParent

  • 相对于某控件的外边距

    android:layout_margin: 指定控件的四周的外部留出一定的边距
    android:layout_marginLeft: 指定控件的左边的外部留出一定的边距
    android:layout_marginTop: 指定控件的上边的外部留出一定的边距
    android:layout_marginRight: 指定控件的右边的外部留出一定的边距
    android:layout_marginBottom: 指定控件的下边的外部留出一定的边距

  • 内边距

    android:padding :指定控件的四周的内部留出一定的边距
    android:paddingLeft: 指定控件的左边的内部留出一定的边距
    android:paddingTop: 指定控件的上边的内部留出一定的边距
    android:paddingRight: 指定控件的右边的内部留出一定的边距
    android:paddingBottom: 指定控件的下边的内部留出一定的边距

  • 补充

    1. android:hint可以设置文本框的提示信息
    2. android:gravity 

      android:gravity属性是对该view 内容的限定.比如一个button 上面的text. 你可以设置该text 在view的靠左,靠右等位置.以button为例,android:gravity="right"则button上面的文字靠右

    1. android:layout_gravity

      android:layout_gravity是用来设置该view相对与起父view 的位置.比如一个button 在linearlayout里,你想把该button放在靠左、靠右等位置就可以通过该属性设置.以button为例,android:layout_gravity="right"则button靠右

    参考自

常用控件

单选按钮(RandioButton)
  • 按钮之间实现互斥

    把它们包裹在RadioGroup控件中

  • RadioGroup可以设置按钮的排列方式
    xml <RadioGroup android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/tv1"> <!--设置orientation属性可以设置里面按钮的布局方式: horizontal(水平排列),vertical(垂直排列)--> <RadioButton android:button="@drawable/rg_selector" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="等额本息"/> <!--RadioButton是:CompoundButton的子类,设置selector选择器得通过button 属性 而不是background属性,--> <!-- 可以通过设置android:checked="true"来使某个按钮默认被选中--> <RadioButton android:button="@drawable/rg_selector" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="等额本金"/> </RadioGroup>

    • 事件处理

      通常是对RadioGroup来进行事件绑定

          RadioGroup rg=findViewById(R.id.rg);
      //        获取RadioGroup,对他绑定setOnCheckedChangeListener事件
              rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                  @Override
      //            i代表的是选中的那个按钮的id值
       //也可以通过group.getCheckedRadioButtonId()来判断
                  public void onCheckedChanged(RadioGroup radioGroup, int i) {
                      if(i==R.id.rb1)
                          textview.setText("你选的是男");
                      else
                          if(i==R.id.rb2)
                              textview.setText("你选的是女");
                  }
              });
      复选按钮(CheckBox)
  • 特点

    同样为CompoundButton的子类,所以和RadioButton差不多,不他需要出现互斥,所以不需要把它们放到一个组里

  • 设置监听
    setOnCheckedChangeListener方法需要实现OnCheckedChangeListener接口

  • 其他

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

String desc = String.format("您%s了这个CheckBox", isChecked?"勾选":"取消勾选");

buttonView.setText(desc);

}
//第一个参数:勾选改变的复选按钮对象 第二个参数:勾选状态
//也可以单独对某个复选框使用isCheck()方法判断他是否被选中
Swith开关
 <Switch
        android:id="@+id/switchtt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textOff="OFF"
        android:textOn="ON"

        android:thumb="@drawable/switch_thumb_selector"
        android:track="@drawable/switch_track_selector" />
    <!--android:thumb属性代表的是滑块的样式。 android:track代表的是滑到的样式-->
  • 事件监听

    //        switch部分
            Switch sw=findViewById(R.id.switch1);
           boolean b= sw.isChecked();
            final TextView tvv=findViewById(R.id.textviewt);
           sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
               @Override
               public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                   if(b==true)
                       tvv.setText("你打开啦这个开关");
                   else
                       tvv.setText("你关闭啦这个开关");
    
               }
    
           });
Spinner下拉列表
  • 使用方式

        <!--设置他的spinnerMode属性来实现下拉列表的展示方式
            android:spinnerMode="dropdown"在下拉框的正下方展示
             android:spinnerMode="dialog"在页面中部以对话框形式展示
            -->
            <Spinner
                android:id="@+id/s1"
                android:spinnerMode="dialog"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/textv1"
                style="@style/spinner"
                ></Spinner>

    java类

    //       创建一个ArrayAdapter数组适配器传入的参数第一个Context对象一般为this ,
    // 第二个布局文件的id,要显示的数据的String数组形式
            ArrayAdapter aa=new ArrayAdapter(this,R.layout.layout3,startArry);
    //        给Spinner设置适配器
            s1.setAdapter(aa);
    

    布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <!--只能包含一个TextView-->
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        style="@style/spinner"
        android:singleLine="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:textAlignment="inherit"
        android:textColor="@color/colorAccent"
        />
    
    • 数组适配器的用法

      一、简单的。

      这样的列表的每一行都只有一行文字。

      // 当然listview 也可以是在layout里写好,然后findViewById()获取出来,这样的话后面就不需setContentView(listview);       
              ListView listview = new ListView(this);   
              ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1);   
              adapter.add("string1");   
              adapter.add("haha");   
              adapter.add("heihei");         
              listview.setAdapter(adapter);   
              setContentView(listview);    

      上面代码中,android.R.layout.simple_expandable_list_item_1是android里已提供的样式,我们也可换成自己的xml。但是需要注意的是这个xml文件仅能有一个textview。连Layout也不能有。否则会报错:ArrayAdapter requires the resource ID to be a TextView

      如layout下有online_user_list_item.xml,它的内容如下:

      <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
      android:layout_width="wrap_content"    
      android:layout_height="wrap_content"     
      android:id="@+id/online_user_list_item_textview" >  
      </TextView>  

      则android.R.layout.simple_expandable_list_item_1换成R.layout.online_user_list_item。

      如果我们想要使用更复杂一点的layout,而不仅是只有一个textview,那就要用下面这种。

      二、样式丰富但内容简单的。

      layout下的online_user_list_item.xml内容如下:

      <?xml version="1.0" encoding="utf-8"?>  
      <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  
          android:layout_width="fill_parent"  
          android:layout_height="wrap_content">  
      <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"  android:id="@+id/online_user_list_item_textview" android:text="TextView"></TextView>  
      <Button  
          android:text="button"  
          android:layout_width="wrap_content"  
          android:layout_height="wrap_content">  
      </Button>  
      </LinearLayout>  

      里面含有的textview是我们想要展示内容的地方。那么构建ArrayAdapter时,应该这样写:

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.online_user_list_item, R.id.online_user_list_item_textview);  

      如果我们需要展示的内容是一仅一个textview承载不了的,还需要其它组件,怎么办?我们可以自定义。

      三、内容丰富的(自定义ArrayAdapter)。

      这就需要写一个类继承自ArrayAdapter并且重写getView方法。上代码:

      public class UserListAdapter extends ArrayAdapter<User> {   
          private int resourceId;   
          public UserListAdapter(Context context, int textViewResourceId, List<User> objects) {   
              super(context, textViewResourceId, objects);   
              this.resourceId = textViewResourceId;   
          }   
      
          @Override  
          public View getView(int position, View convertView, ViewGroup parent){   
              User user = getItem(position);   
              LinearLayout userListItem = new LinearLayout(getContext());   
              String inflater = Context.LAYOUT_INFLATER_SERVICE;    
              LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);    
              vi.inflate(resourceId, userListItem, true);   
              TextView tvUsername = (TextView)userListItem.findViewById(R.id.tv_user_list_username);   
              TextView tvAskedNum = (TextView)userListItem.findViewById(R.id.tv_user_list_askednum);   
              TextView tvLastMsg = (TextView)userListItem.findViewById(R.id.tv_user_list_lastmsg);   
              tvUsername.setText(user.getUsername());   
              tvAskedNum.setText(String.valueOf(user.getAskedNum()));   
              tvLastMsg.setText(user.getLastMsg());   
              return userListItem;   
          }   
      }  

      activity里就这样写

      List<User> users = new ArrayList<User>();   
             User user = new User();   
             user.setAskedNum(8);   
             user.setLastMsg("hello");   
             user.setUsername("pxx");   
             users.add(user);   
             users.add(user);   
             users.add(user);   
             UserListAdapter adapter = new UserListAdapter(this,R.layout.online_user_list_item,users);   
             listview.setAdapter(adapter);  

      参考自

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄