(android控件)界面布局實現GridView(網格效果)


1 需要實現的效果

處於業務的需求,需要實現網格效果,具體來講 屏幕中一行等分兩塊,每一塊中居中放一個圖片。

補充要求:考慮界面設計時,通用於不同分辨率手機屏幕,則不能通過固定位置布局

 

實現界面效果圖:

 

2 神奇的控件屬性:layout_weight

layout_weight 屬性

屬性的意義: 設置額外分配給控件的空間,默認為0 ,控件保持不變 ,屬性大於0,則將屏幕剩余的控件分配給控件。

 

3 解決思路

     [一般的實現的方式]

             設置一個橫向布局,放置控件,並設置控件之間的間隔,但對於不同尺寸的屏幕,要做多套界面

 

    [本文的解決思路]

           設置一個橫向布局,在布局中添加數個 線性布局,並統一設置屬性 layout_weight =1,居中 ,在線性布局中放入需要的控件,這樣利用layout_weight屬性讓線性布局等分,且可以保證需要的控件不走樣。

 

 

4 實戰實現代碼

 

   <LinearLayout
android:id="@+id/linearLayout1"
android:layout_width
="fill_parent"
android:orientation
="horizontal"
android:layout_marginTop
="15dp"
android:layout_height
="wrap_content" >

<LinearLayout
android:id="@+id/linearLayout_n1"
android:layout_width
="fill_parent"
android:orientation
="horizontal"
android:layout_weight
="1"
android:gravity
="center"
android:layout_height
="wrap_content" >
<ImageView
android:id="@+id/img_1"
android:adjustViewBounds
="true"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:padding
="4dp"
android:focusable
="true"
android:clickable
="true"
android:tag
="1"
android:background
="@drawable/imgselect"
android:src
="@drawable/sl1_1" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout_n2"
android:layout_width
="fill_parent"
android:layout_weight
="1"
android:orientation
="horizontal"

android:gravity
="center"
android:layout_height
="wrap_content" >
<ImageView
android:id="@+id/img_2"
android:adjustViewBounds
="true"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:padding
="4dp"
android:tag
="2"
android:focusable
="true"
android:clickable
="true"
android:background
="@drawable/imgselect"
android:src
="@drawable/sl1_2" />
</LinearLayout>
</LinearLayout>

 

           


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM