【轉】EditText大小(長寬)的多種設置方式----不錯


原文網址:http://orgcent.com/android-edittext-ems-layout-minwidth-maxheight/

EditText大小的設置有多種方式,要想對每種方式運用自如必須熟練掌握它們。下面將對其詳細描述:

1、layout_width和layout_height
layout_width告訴父容器EditText需要的寬度,layout_height則設置需要的高度。單位建議為dp,關於Android中單位轉換問題可以參考Android根據分辨率進行單位轉換-(dp,sp轉像素px)

2、minWidth/maxHeight和minHeight/maxHeight
這些屬性用來動態的限制EditText的大小,應用場景有如下幾種情況:
1)在沒有內容情況下,通過min系列保證控件的最小寬高,保持界面整體美觀。
2)在內容過多情況下,使用max系列來限制控件大小,確保不影響界面上的其他控件。
PS:還可以使用minLines和maxLines來限制內容顯示的行數。

3、ems、minEms和maxEms
ems指的是字體的寬度。此屬性在Android中用來設置EditText的寬度,即設置EditText為n個字符的寬度。
PS:android:layout_width必須為wrap_content,否則ems將無效。

參考代碼:

<EditText 
       android:id="@+id/etTest" 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" android:ems="4"/>
 
原文網址:http://www.jb51.net/article/32794.htm

Android取消EditText自動獲取焦點默認行為

在項目中,一進入一個頁面, EditText默認就會自動獲取焦點,很是郁悶,Android 如何讓EditText不自動獲取焦點?於是搜集整理一番,曬出來和大家分享,希望對你們有所幫助

那么如何取消這個默認行為呢? 
在網上找了好久,有點 監聽軟鍵盤事件,有點 調用 clearFouse()方法,但是測試了都沒有! xml中也找不到相應的屬性可以關閉這個默認行為 
解決之道:在EditText的父級控件中找一個,設置成 
代碼如下:

android:focusable="true" 
android:focusableInTouchMode="true" 

這樣,就把EditText默認的行為截斷了! 

<LinearLayout 
style="@style/FillWrapWidgetStyle" 
android:orientation="vertical" 
android:background="@color/black" 
android:gravity="center_horizontal" 
android:focusable="true" 
android:focusableInTouchMode="true" 

<ImageView 
android:id="@+id/logo" 
style="@style/WrapContentWidgetStyle" 
android:background="@drawable/dream_dictionary_logo" 
/> 
<RelativeLayout 
style="@style/FillWrapWidgetStyle" 
android:background="@drawable/searchbar_bg" 
android:gravity="center_vertical" 

<EditText 
android:id="@+id/searchEditText" 
style="@style/WrapContentWidgetStyle" 
android:background="@null" 
android:hint="Search" 
android:layout_marginLeft="40dp" 
android:singleLine="true" 
/> 
</RelativeLayout> 
</LinearLayout> 
 


免責聲明!

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



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