進階篇-使用者介面:3.android中的基本布局-layout

來源:互聯網
上載者:User

標籤:

1.RelativeLayout

      在RelativeLayout中,組件可以根據父級邊界進行定位,除了這樣的定位,也可以相對其他控制項進行定位。特點是,可以隨意拖動空間,拖到哪裡就停到哪裡。

代碼中添加子物件:

import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.RelativeLayout;import android.widget.TextView;public class Main2Activity extends AppCompatActivity {    private RelativeLayout root;    private TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        root = new RelativeLayout(this); //初始化一個relativelayout        setContentView(root);//使該activity載入這個布局        tv = new TextView(this);        tv.setText("安卓入門進階");        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);//建立布局參數對象        lp.leftMargin = 200;//調整textView的位置        lp.topMargin = 500;        root.addView(tv,lp);//將textView添加到layout中    }}

2.FramLayout

      framlayout特點為無論拖進去多少個控制項對象,都可以重疊。並且只有九個位置可以放置。它的特點與功能在relativeLayout中都可以實現,但是framlayout更加輕量級,並且使用方便簡潔。

舉個例子,在一個framlayout中重疊放置兩張圖片,點擊圖片互相切換。

java源碼:

import android.support.v4.app.Fragment;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.FrameLayout;import android.widget.ImageView;public class Main2Activity extends AppCompatActivity {    private FrameLayout root;    private ImageView iva;    private ImageView ivb;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main2);        root = (FrameLayout) findViewById(R.id.root); //根據id擷取到父容器framlayout        iva = (ImageView) findViewById(R.id.iva);//擷取組件        ivb = (ImageView) findViewById(R.id.ivb);        root.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {          //點擊執行圖片的切換               if(iva.getVisibility()==View.VISIBLE){                        showB();               }else{                   showA();               }            }        });    }    private void showA() {   //顯示A圖片的方法         iva.setVisibility(View.VISIBLE);         ivb.setVisibility(View.INVISIBLE);    }    private void showB() {   //顯示B圖片的方法        iva.setVisibility(View.INVISIBLE);        ivb.setVisibility(View.VISIBLE);    }}

xml源碼

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:id="@+id/root"    tools:context="com.example.lzc.myapplication.Main2Activity">    <ImageView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:src="@drawable/img1"        android:id="@+id/iva"/>    <ImageView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:src="@drawable/img1"        android:id="@+id/ivb"/></FrameLayout>

3.LinearLayout

     linerlayout是安卓中的線性布局。使其子物件水平方向或者垂直方向一字排開。其中的屬性android:orientation可以控制排列的方向。android:orientation="vertical"為垂直排列。android:orientation="horizontal"為水平排列。線上性布局中有一個重要的屬性為比重。也就是layout_weight,它規定了分割父級容器的比例。

     如果兩個控制項,控制項A有layout_weight,控制項B沒有,那麼B的寬度就是它本身的寬度,剩下的空間全部給A。

例子布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <EditText            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"/>        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="go"/>    </LinearLayout>    <WebView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"></WebView></LinearLayout>

 

進階篇-使用者介面:3.android中的基本布局-layout

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.