閱讀151 返回首頁    go 技術社區[雲棲]


Android下實現控件的疊加顯示

在做手機軟件時,經常會遇到空間疊加顯示的問題,比如在一個圖片的buttom|center上添加一個名字或者是加上log圖片,這樣的圖片當然可以通過PS等軟件進行處理之後直接setSource給ImageView,但是這種方式隻能處理靜態的事務。如果需要動態的在某些圖片上添加其他的物件的話,就需要用到我們今天介紹的FrameLayout。

FrameLayout沒有標準的中文翻譯,但是就功能來看,“單幀布局”貌似要比“框架布局”更貼切一些。在FrameLayout上麵添加控件,就好像是向一張畫布上添加一張一張的貼片,後麵的控件會覆蓋在之前的控件之上。如果後麵的控件比較小,就可以很容易的實現上麵所述的控件疊加的需求了。至於怎麼使用,在網上應該會有比較多的文章介紹。這裏就不在贅述。

可能遇到的問題:

1、在圖片A的buttom|center位置添加圖片B。首先在FrameLayout上麵加上圖片A,然後再添加圖片B。同時需要設置圖片B的margin以在合適的位置。但是在測試過程中發現,如果直接在FrameLayout直接設置B的margin,效果不會顯示。解決這個問題的一個比較簡單的辦法就可以在圖片B和FrameLayout之間疊加一層Layout,比如LinearLayout等。這樣就可以實現上述需求了。

如:

<FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dip"
        android:orientation="vertical" >
        <Gallery
        android:
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
        <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
         />
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:layout_marginBottom="20dip"
            android:layout_marginLeft="50dip"
            android:layout_marginRight="50dip"
            android:layout_gravity="bottom"
            >
			<TextView
                android:
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:textColor="#000" />
            <Button
                android:
                android:layout_width="wrap_content"
				android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_alignParentRight="true"
                android:text="購入囊中"
                android:textColor="#fff"
                android:background="@drawable/buy" />

            
        </RelativeLayout>
    </FrameLayout>


最後更新:2017-04-02 17:28:39

  上一篇:go Introducing AQuery: jQuery for Android
  下一篇:go 掌握MySQL如何使用臨時表,避免踩中性能地雷