Hy ....我在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="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>
关于我的主要活动:
public class AddRemoveMainActivity extends Activity {
Button add,del;
LinearLayout ll;
private String[] spinnerContent = {"Ikan","Udang","Kepiting","Kerang"};
private int count = 0;
private int line = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_remove_main);
ll = (LinearLayout) findViewById(R.id.main_layout);
}
public void addItems(View view)
{
count++;
final LinearLayout frame = new LinearLayout(this);
frame.setOrientation(LinearLayout.VERTICAL);
frame.setId(count);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,spinnerContent);
final TableRow row1 = new TableRow(this);
Spinner spin = new Spinner(this);
TextView jenis = new TextView(this);
TextView keterangan = new TextView(this);
TextView total = new TextView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 13, 17, 9);
spin.setAdapter(adapter);
jenis.setText("Jenis : "+Integer.toString(count));
//jenis.setLayoutParams(params);
keterangan.setText("Keterangan : ");
total.setText("Jumlah(kg) :");
row1.addView(jenis);
row1.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
//ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)jenis.getLayoutParams();
//mlp.setMargins(37, 13, 17, 0);
row1.addView(spin);
frame.addView(row1);
ll.addView(frame, line);
line++;
}
public void deleteItems(View view)
{
if(count > 0)
{
final LinearLayout temp = (LinearLayout)ll.findViewById(count);
temp.removeAllViews();
ll.removeView(temp);
count--;
line--;
}
}
从mainActivity中我可以看到,我已经将textview(jenis)和spinner(spin)添加到tableRow,然后我将tableRow添加到linearLayout(frame)然后将该帧添加到来自xml的linearLayout中两个按钮上方的文件(Add&amp; Del按钮)....... 我的问题是我只想在textview(jenis)和(Spinner)旋转之间添加一个边距。正如您从mainActivity中可以看到的那样,我尝试过使用:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 13, 17, 9);
...
...
...
jenis.setLayoutParams(params);
但是这个没有用......我尝试了另一种方式,但它也不起作用:
//ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)jenis.getLayoutParams();
//mlp.setMargins(37, 13, 17, 0);
所以任何想法或其他方式为我的textview添加保证金...... ??? 在此先感谢... :-)
答案 0 :(得分:2)
尝试下面的代码,让我知道它是否有效
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);
答案 1 :(得分: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);
}
答案 2 :(得分: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);
// NJ forgot the last important step, set LayoutParams on the TextView:
v.setLayoutParams(mlp);
}