Aru*_*run 8 java android ellipse
在表格布局中我有一个tablerow,在该tablerow中我有6个编辑文本框,我想为这6个编辑文本框设置布局边距
TableLayout t1=(TableLayout)findViewById(R.id.table_layout01);
TableRow tr1=new TableRow(inventory.this);
tr1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tr1.setBackgroundColor(Color.BLACK);
EditText ed6=new EditText(inventory.this);
//ed6.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
/*ViewGroup.MarginLayoutParams editmargin=new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.FILL_PARENT,ViewGroup.MarginLayoutParams.WRAP_CONTENT);
editmargin.setMargins(leftMargin, rightMargin, topMargin, bottomMargin);*/
ed6.setTextColor(Color.BLACK);
ed6.setBackgroundColor(Color.WHITE);
ed6.setText("1");
tr1.addView(ed6);
EditText ed7=new EditText(inventory.this);
//ed7.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed7.setTextColor(Color.BLACK);
ed7.setBackgroundColor(Color.WHITE);
ed7.setText("2");
tr1.addView(ed7);
EditText ed8=new EditText(inventory.this);
//ed8.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed8.setTextColor(Color.BLACK);
ed8.setBackgroundColor(Color.WHITE);
ed8.setText("3");
tr1.addView(ed8);
EditText ed9=new EditText(inventory.this);
//ed9.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed9.setTextColor(Color.BLACK);
ed9.setBackgroundColor(Color.WHITE);
ed9.setText("4");
tr1.addView(ed9);
EditText ed10=new EditText(inventory.this);
//ed10.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed10.setTextColor(Color.BLACK);
ed10.setText("5");
ed10.setBackgroundColor(Color.WHITE);
tr1.addView(ed10);
EditText ed11=new EditText(inventory.this);
//ed11.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed11.setTextColor(Color.BLACK);
ed11.setText("6");
ed11.setBackgroundColor(Color.WHITE);
tr1.addView(ed11);
t1.addView(tr1);
Run Code Online (Sandbox Code Playgroud)
首先你应该知道的事情:根据官方的Android开发页面,视图(和TextView派生自View)不支持Margin的设置,但是ViewGroups(例如LinearLayout,RelativeLayout等等).
那你可以做的是以下几点:
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.setMargins(5, 5, 5, 5);
TextView view = new TextView(this);
view.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)
这会将所有孩子的边距设置为5像素 - 我尝试了它并且它对我有效(尽管LinearLayout有垂直对齐).试一试,让我知道我是否可以进一步帮助:).
干杯,
Ready4Fajir
编辑:
我会尝试使用下面的 XML(当然,您会更新 id 等)。xml 中的“神奇之处”在于它将所有可用宽度均匀地分布在 之间TextView(以及EditText第二行上的 )。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The first "row" -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView 01"
android:id="@+id/textView01" />
<!-- Here you'd add your other five TextView's accordingly -->
</LinearLayout>
<!-- The second "row" -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView 01"
android:id="@+id/editText01" />
<!-- Here you'd add your other five EditText's accordingly -->
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
然后,您可以在 Java 代码中访问 EditText 视图,例如:
EditText editText01 = (EditText) findViewById(R.id.editText01);
editText01.setText("1");
Run Code Online (Sandbox Code Playgroud)
我现在忽略了您需要以编程方式创建 EditText 的事实。您真的需要用 Java 创建它们吗?(为什么?)
旧答案:
如果您只想将布局边距设置为 EditText 视图,我想您可以使用变量setMargins(left, top, right, bottom)上的函数调用LayoutParams。
int left = 6;
int top = 12;
int right = 6;
int bottom = 6;
TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
EditText edXY = new EditText(inventory.this);
edXY.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)
如果您最终希望在表行中的六个 EditText 视图之间均匀分配所有可用空间,我建议您查看以下帖子:2-column TableLayout with 50%正好为每列