android imageview圖片居中技巧應(yīng)用
更新時(shí)間:2012年11月24日 15:02:46 作者:
做UI布局,尤其是遇到比較復(fù)雜的多重LinearLayout嵌套,常常會被一些比較小的問題困擾上半天,可是無論怎樣設(shè)置layout_gravity屬性,都無法達(dá)到效果
做UI布局,尤其是遇到比較復(fù)雜的多重LinearLayout嵌套,常常會被一些比較小的問題困擾上半天,比如今天在使用ImageView的時(shí)候,想讓其居中顯示,可是無論怎樣設(shè)置layout_gravity屬性,都無法達(dá)到效果,部分代碼如下:
[java]
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
android:padding="20dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="108dp"
android:layout_height="108dp"
android:orientation="vertical"
android:background="#3399ff">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@drawable/menu_icon__mail"
android:layout_gravity="center"/>
</LinearLayout>
于是乎四處找資料尋求解決的方式,原來是父類視圖的屬性沒有設(shè)置的原因,將父類視圖設(shè)置為居中邊可解決,即android:gravity="center":
[java]
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
android:padding="20dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="108dp"
android:layout_height="108dp"
android:orientation="vertical"
android:gravity="center"
android:background="#3399ff">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@drawable/menu_icon__mail"
android:layout_gravity="center"/>
</LinearLayout>
UI設(shè)計(jì)方面往往很小的細(xì)節(jié),很簡單的問題,有的時(shí)候就是會讓你煩上好一陣子,不過自己動(dòng)手多多設(shè)計(jì)經(jīng)驗(yàn)多了,解決起來就簡單鳥!
[java]
復(fù)制代碼 代碼如下:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
android:padding="20dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="108dp"
android:layout_height="108dp"
android:orientation="vertical"
android:background="#3399ff">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@drawable/menu_icon__mail"
android:layout_gravity="center"/>
</LinearLayout>
于是乎四處找資料尋求解決的方式,原來是父類視圖的屬性沒有設(shè)置的原因,將父類視圖設(shè)置為居中邊可解決,即android:gravity="center":
[java]
復(fù)制代碼 代碼如下:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
android:padding="20dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="108dp"
android:layout_height="108dp"
android:orientation="vertical"
android:gravity="center"
android:background="#3399ff">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@drawable/menu_icon__mail"
android:layout_gravity="center"/>
</LinearLayout>
UI設(shè)計(jì)方面往往很小的細(xì)節(jié),很簡單的問題,有的時(shí)候就是會讓你煩上好一陣子,不過自己動(dòng)手多多設(shè)計(jì)經(jīng)驗(yàn)多了,解決起來就簡單鳥!
您可能感興趣的文章:
- Android文本視圖TextView實(shí)現(xiàn)跑馬燈效果
- Android使用ViewBinding的詳細(xì)步驟(Kotlin簡易版)
- Android使用ViewPager實(shí)現(xiàn)啟動(dòng)引導(dǎo)頁效果
- Android實(shí)現(xiàn)view拖動(dòng)到任意位置
- Android OpenGL入門之GLSurfaceView
- android TextView設(shè)置中文字體加粗實(shí)現(xiàn)方法
- android listview優(yōu)化幾種寫法詳細(xì)介紹
- android WebView加載html5介紹
- Android TextView設(shè)置背景色與邊框的方法詳解
- Android view繪制流程詳解
相關(guān)文章
Android 實(shí)現(xiàn)秒轉(zhuǎn)換成時(shí)分秒的方法
這篇文章主要介紹了Android 實(shí)現(xiàn)秒轉(zhuǎn)換成時(shí)分秒的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Android Studio通過Artifactory搭建本地倉庫優(yōu)化編譯速度的方法
這篇文章主要介紹了Android Studio通過Artifactory搭建本地倉庫優(yōu)化編譯速度的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Android(2.2/2.3系統(tǒng))Gallery解決默認(rèn)和橫豎屏切換選中狀態(tài)問題
本文主要介紹Android Gallery,在Android開發(fā)過程中肯定會遇到版本不同,在開發(fā)過程中需要調(diào)整的,這里針對Android(2.2/2.3系統(tǒng)版本) Gallery解決默認(rèn)和橫豎屏切換選中狀態(tài)問題2016-07-07
Android四大組件:Activity/Service/Broadcast/ContentProvider作用示例
Android是一種基于Linux,自由及開放源代碼的操作系統(tǒng),Android分為四個(gè)層,從高層到底層分別是應(yīng)用程序?qū)印?yīng)用程序框架層、系統(tǒng)運(yùn)行庫層和Linux內(nèi)核層,Android有四大基本組件:Activity、Service服務(wù)、BroadcastReceiver廣播接收器、Content Provider內(nèi)容提供者2023-11-11
Android入門之系統(tǒng)設(shè)置Configuration類的使用教程
這篇文章主要給大家介紹一下Configuration類的使用,Configuration類是用來描述手機(jī)設(shè)備的配置信息的,比如屏幕方向,?觸摸屏的觸摸方式等,感興趣的可以了解一下2022-12-12
Android開發(fā)筆記之:消息循環(huán)與Looper的詳解
本篇文章是對Android中消息循環(huán)與Looper的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android畫圖并保存圖片的具體實(shí)現(xiàn)代碼
這篇文章介紹了在Android中畫圖并保存圖片的實(shí)例,以下是具體的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2013-07-07
Android開發(fā)中amera2 Preview使用詳解
這篇文章主要介紹了Android開發(fā)中amera2 Preview使用詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09

