問(wèn)題描述
我完全無(wú)法在 TableRow 中獲取固定寬度的 EditText 小部件.我正在嘗試以相同的寬度(大約 20dip)并排放置兩個(gè) EditText,但無(wú)論我嘗試設(shè)置哪個(gè)屬性并將第一個(gè) EditText 設(shè)置為很長(zhǎng)并且顯然無(wú)法調(diào)整大小.
I'm completely stuck trying to get fixed width EditText widgets in a TableRow. I am attempting to place two EditText side by side with equal width (about 20dip) but no matter which property I try and set the first EditText is way to long and apparently cannot be resized.
非常感謝:
<TableRow
android:layout_height="wrap_content"
android:baselineAligned="false"
android:id="@+id/tableRow3"
android:gravity="center"
android:stretchColumns="1"
android:layout_width="match_parent">
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:paddingLeft="36dip">
</TextView>
<EditText
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:inputType="number"
android:layout_width="20dip">
</EditText>
<EditText
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:inputType="number"
android:layout_width="wrap_content">
<requestFocus></requestFocus>
</EditText>
</TableRow>
推薦答案
我不知道 TableLayout 是不是最好的方法,它可能很麻煩,除非你顯示大量數(shù)據(jù)并且需要使用它.
I don't know that a TableLayout is the best way to do this, it can be cumbersome unless you're displaying large amounts of data and need to use it.
我發(fā)現(xiàn)確保表單對(duì)象按照我想要的方式分配長(zhǎng)度的最佳方法之一是使用權(quán)重而不是顯式聲明寬度.
One of the best ways I've found to ensure that form objects have length distributed the way I want them is by using weight rather than explicitly declaring width.
嘗試以下方法:
<LinearLayout ... android:orientation="horizontal" ...
android:layout_width="match_parent" android:layout_height="wrap_content"
<TextView ... android:layout_width="0dp" ... android:layout_weight="50" />
<TextView ... android:layout_width="0dp" ... android:layout_weight="50" />
</LinearLayout>
確保將布局寬度聲明為 0,這將使布局填充到權(quán)重.
Make sure to declare the layout width as 0, this will let the layout fill to the weight.
這應(yīng)該會(huì)在屏幕上創(chuàng)建兩個(gè)相鄰的 TextView,它們都占據(jù)了 50% 的屏幕.你可以玩不同的百分比.您還可以使用 LinearLayout 作為占位符,其權(quán)重為您希望放置的任何 %.
This should create two TextViews next to each other on the screen, both filling 50% of the screen. You can play with different percentages. You can also use a LinearLayout as a placeholder with a weight of whatever % you would like to place hold.
確保您的權(quán)重"加起來(lái)為 100,以確保視圖看起來(lái)完全符合您的要求.這不是必需的,但知道它將占據(jù)屏幕寬度的百分比是一個(gè)很好的約定.
Make sure that your "weights" add up to 100 in order to ensure the view will look exactly as you want it to. It's not necessary, but it's a good convention to know what % of the screen width it will take up.
這篇關(guān)于TableRow中的Android固定寬度EditText的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!