我完全无法在 TableRow 中获取固定宽度的 EditText 小部件.我正在尝试以相同的宽度(大约 20dip)并排放置两个 EditText,但无论我尝试设置哪个属性并将第一个 EditText 设置为很长并且显然无法调整大小.
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 是不是最好的方法,它可能很麻烦,除非你显示大量数据并且需要使用它.
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.
我发现确保表单对象按照我想要的方式分配长度的最佳方法之一是使用权重而不是显式声明宽度.
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,这将使布局填充到权重.
Make sure to declare the layout width as 0, this will let the layout fill to the weight.
这应该会在屏幕上创建两个相邻的 TextView,它们都占据了 50% 的屏幕.你可以玩不同的百分比.您还可以使用 LinearLayout 作为占位符,其权重为您希望放置的任何 %.
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.
确保您的权重"加起来为 100,以确保视图看起来完全符合您的要求.这不是必需的,但知道它将占据屏幕宽度的百分比是一个很好的约定.
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.
这篇关于TableRow中的Android固定宽度EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!