Android小结(1)
生活随笔
收集整理的這篇文章主要介紹了
Android小结(1)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這篇文章主要是介紹線性布局和約束布局的一些常用屬性,以及textview的一些常用的用法。主要還是得多實踐。
文章目錄
- (1)線性布局
- (2)相對布局
- (3)小結
(1)線性布局
主要的屬性:如代碼所示,在代碼中解釋屬性。
<?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"//設置布局是垂直布局還是橫向布局android:paddingTop="20dp"//設置布局管理器內組件距離父物體頂部的距離><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="hello world "></TextView><LinearLayoutandroid:layout_width="200dp"android:layout_height="200dp"android:padding="20dp"android:orientation="vertical"android:gravity="center"//設置布局管理器內的組件的對齊方向><Viewandroid:layout_height="50dp"android:layout_width="50dp"android:layout_weight="1"android:background="@color/black"></View><Viewandroid:layout_height="50dp"android:layout_width="50dp"android:layout_weight="1"android:background="@color/black"android:layout_marginTop="30dp"//設置距離上個組件top端的位置></View></LinearLayout></LinearLayout>我們通過這段代碼中不難發現,其實margin和padding屬性是有些類似的,并且都可以設置上下左右的距離,我們可以記住大概的意思就可以了,主要還是得多實踐,多實踐,多實踐,重要的事說三遍!!!
(2)相對布局
相對布局我們也采用在代碼中解釋屬性,來帶大家了解他的一些屬性
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:paddingTop="20dp"><Buttonandroid:id="@+id/btm"//每個組件都設置一個屬于自己的id,然后布局的話我們可以減少嵌套布局,可以根據周圍的組件來進行布局。android:layout_width="100dp"android:layout_height="100dp"android:layout_centerInParent="true"android:text="Button"></Button><RelativeLayoutandroid:layout_width="300dp"android:layout_height="300dp"android:layout_below="@id/btm"android:layout_marginTop="10dp"android:background="#ffff00"android:gravity="center"><TextViewandroid:id="@+id/t1"android:layout_width="100dp"android:layout_height="100dp"android:text="textview1"android:textSize="20dp"></TextView><TextViewandroid:id="@+id/t2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/t1"android:layout_marginTop="10dp"android:text="textview1"></TextView><TextViewandroid:id="@+id/t3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/t2"android:layout_marginTop="10dp"android:text="textview1"></TextView></RelativeLayout></RelativeLayout>對相對布局的看法:最大的區別就是可以給每個組件一個id,可以根據周圍的控件來進行布局,當然,線性布局和相對布局之間是可以相互嵌套的,可以更靈活吧,這里可能對相對布局介紹的簡單了點,我們后續會用到什么,再補充進來。
(3)小結
這兩種布局可能沒有約束布局好用,個人認為,但是網上的老師,和學校的老師都說,這兩種布局是現在常用的兩種,我們就先學好這兩種布局,用好這兩種布局,到時候上手約束布局就會很容易了(約束布局yyds),我在下個禮拜準備用這兩個布局寫一個界面,可能會用到更多的東西,盡請期待!
總結
以上是生活随笔為你收集整理的Android小结(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Input类,Vector3实例
- 下一篇: Android跑马灯