gpt4 book ai didi

java - 如何在 tablerow 内以编程方式设置 textview 的边距

转载 作者:行者123 更新时间:2023-12-01 22:19:44 26 4
gpt4 key购买 nike

嘿...我正在 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>

关于我的主要 Activity :

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);

关于java - 如何在 tablerow 内以编程方式设置 textview 的边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30137417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com