Java 如何在tablerow中以编程方式设置textview的边距
嗨。。。。我正在做一个关于eclipse的项目。在本项目中,我制作了如下xml布局:Java 如何在tablerow中以编程方式设置textview的边距,java,android,xml,android-layout,Java,Android,Xml,Android Layout,嗨。。。。我正在做一个关于eclipse的项目。在本项目中,我制作了如下xml布局: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="ma
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddRemoveMainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/main_layout"
>
<!-- I will add some view programatically in this line -->
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/b_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:onClick="addItems"
/>
<Button
android:id="@+id/b_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Del"
android:onClick="deleteItems"
/>
</TableRow>
</LinearLayout>
</ScrollView>
但这一个不是工作。。。我尝试过另一种方法,但也不管用:
//ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)jenis.getLayoutParams();
//mlp.setMargins(37, 13, 17, 0);
那么,有没有给我的文本视图添加边距的想法或其他方法。。。???
提前感谢…:-) 请添加此方法并检查。它对我有用
public static void setRunTimeMargin(View v, int left, int top, int right,
int bottom) {
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) v
.getLayoutParams();
mlp.setMargins(left, top, right, bottom);
}
请添加此方法并检查。它对我有用
public static void setRunTimeMargin(View v, int left, int top, int right,
int bottom) {
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) v
.getLayoutParams();
mlp.setMargins(left, top, right, bottom);
}
试试下面的代码,让我知道它是否有效
android.widget.TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 13, 17, 9);
row1.addView(jenis, layoutParams);
//ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)jenis.getLayoutParams();
//mlp.setMargins(37, 13, 17, 0);
row1.addView(spin, layoutParams);
试试下面的代码,让我知道它是否有效
android.widget.TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 13, 17, 9);
row1.addView(jenis, layoutParams);
//ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)jenis.getLayoutParams();
//mlp.setMargins(37, 13, 17, 0);
row1.addView(spin, layoutParams);
这是有效的:
public static void setRunTimeMargin(View v, int left, int top, int right,
int bottom) {
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) v
.getLayoutParams();
mlp.setMargins(left, top, right, bottom);
// NJ forgot the last important step, set LayoutParams on the TextView:
v.setLayoutParams(mlp);
}
这是有效的:
public static void setRunTimeMargin(View v, int left, int top, int right,
int bottom) {
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) v
.getLayoutParams();
mlp.setMargins(left, top, right, bottom);
// NJ forgot the last important step, set LayoutParams on the TextView:
v.setLayoutParams(mlp);
}
这个对我来说很好。。谢谢多拉…:-)我刚意识到我在使用LinearLayout.LayoutParams。。。所以只需将其更改为TableRow.LayoutParams…;-)谢谢……这个对我来说很好用。。谢谢多拉…:-)我刚意识到我在使用LinearLayout.LayoutParams。。。所以只需将其更改为TableRow.LayoutParams…;-)感谢…使用mlp.setMargin(左、上、右、下)时出现异常…eclipse说这是空指针异常。。。。但是谢谢你的回答,尼莱什先生…:-)使用mlp.setMargin(左、上、右、下)时出现异常…eclipse说这是空指针异常。。。。但是谢谢你的回答,尼莱什先生…:-)我把我所有的源代码都放在这里:我把我所有的源代码都放在这里: