我用两个单元格和一个分隔符创建表格。为每个元素设置LayoutParams。
设置边距和宽度但不起作用。我为TextView设置了LayoutParams并得到了这个错误,
01-20 07:16:14.320: E/AndroidRuntime(4144): FATAL EXCEPTION: main
01-20 07:16:14.320: E/AndroidRuntime(4144): Process: bairro.lettersgenerator, PID: 4144
01-20 07:16:14.320: E/AndroidRuntime(4144): java.lang.ArithmeticException: divide by zero
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.widget.TableLayout.mutateColumnsWidth(TableLayout.java:587)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.widget.TableLayout.shrinkAndStretchColumns(TableLayout.java:576)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.widget.TableLayout.measureVertical(TableLayout.java:474)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.widget.TableLayout.onMeasure(TableLayout.java:439)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.View.measure(View.java:16497)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.View.measure(View.java:16497)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.View.measure(View.java:16497)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.View.measure(View.java:16497)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.View.measure(View.java:16497)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-20 07:16:14.320: E/AndroidRuntime(4144): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.View.measure(View.java:16497)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.Choreographer.doFrame(Choreographer.java:544)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.os.Handler.handleCallback(Handler.java:733)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.os.Handler.dispatchMessage(Handler.java:95)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.os.Looper.loop(Looper.java:136)
01-20 07:16:14.320: E/AndroidRuntime(4144): at android.app.ActivityThread.main(ActivityThread.java:5001)
01-20 07:16:14.320: E/AndroidRuntime(4144): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 07:16:14.320: E/AndroidRuntime(4144): at java.lang.reflect.Method.invoke(Method.java:515)
01-20 07:16:14.320: E/AndroidRuntime(4144): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-20 07:16:14.320: E/AndroidRuntime(4144): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-20 07:16:14.320: E/AndroidRuntime(4144): at dalvik.system.NativeStart.main(Native Method)
我也设置了具有固定宽度的分隔符,但它什么也没做
TableLayout table = new TableLayout(this);
TableLayout.LayoutParams table_params = new TableLayout.LayoutParams();
table_params.width = LayoutParams.MATCH_PARENT;
table_params.height = LayoutParams.WRAP_CONTENT;
table_params.setMargins(5, 5, 5, 5);
table.setLayoutParams(table_params);
table.setStretchAllColumns(true);
table.setBackgroundColor(new Color().rgb(255, 255, 130));
PrintLog.p("i", ((TableLayout.LayoutParams)table.getLayoutParams()).leftMargin);
TableRow tr = new TableRow(this);
TableRow.LayoutParams tr_params = new TableRow.LayoutParams();
tr_params.leftMargin = 1;
tr.setLayoutParams(tr_params);
tr.setBackgroundColor(new Color().rgb(0, 255, 0));
TextView txt_cell_1 = new TextView(this);
//txt_cell_1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
txt_cell_1.setGravity(Gravity.CENTER);
txt_cell_1.setTextSize(40);
txt_cell_1.setText("1");
TextView txt_cell_2 = new TextView(this);
//txt_cell_2.setLayoutParams(txt_cell_1.getLayoutParams());
txt_cell_2.setGravity(Gravity.CENTER);
txt_cell_2.setTextSize(40);
txt_cell_2.setText("1");
LinearLayout devider_cells = new LinearLayout(this);
devider_cells.setOrientation(LinearLayout.VERTICAL);
devider_cells.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams devider_cells_params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f);
devider_cells.setLayoutParams(devider_cells_params);
devider_cells.setBackgroundColor(new Color().rgb(255, 255, 255));
tr.addView(txt_cell_1);
tr.addView(devider_cells);
tr.addView(txt_cell_2);
table.addView(tr);
setContentView(table);
有什么建议吗?
答案 0 :(得分:0)
这行代码导致错误(你需要替换它!!!)。这是我在提到这个link
之后理解的table.setLayoutParams(table_params);
table.setStretchAllColumns(true);
您需要将其更改为
TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
希望对此有所帮助!
答案 1 :(得分:0)
实际上你可以使用TableRow.LayoutParams是一种特殊情况,它在没有宽度和高度的情况下有效。这似乎没问题。
我关心的是这条线: txt_cell_2.setLayoutParams(txt_cell_1.getLayoutParams());
这可能不是Android喜欢的。如果您真的想通过对两个TextView(我不推荐)使用单个LayoutParams来避免重复初始化,请尝试以下操作:
ViewGroup.LayoutParams commonParms
= new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
....
txt_cell_1.setLayoutParams(commonParms);
....
txt_cell_2.setLayoutParams(commonParms);
我想,在这种情况下,系统每次都会将ViewGroup.LayoutParams转换为LinearLayout.LayoutParam,因此您将拥有两个LayoutParam"""掩盖" :)