[英]How to set margin of textview programatically within tablerow
嗨,...我正在做一个日蚀项目。 在这个项目中,我进行了如下的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中。两个按钮(“添加”和“删除”按钮).......我的问题是我只想在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添加边距... ??? 提前致谢... :-)
尝试以下代码,让我知道它是否有效
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.