Android中的setMargin方法

时间:2015-02-18 06:15:39

标签: android

看看代码:

public class MainActivity extends ListActivity {

    public static ArrayList<Color> colorList;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        init();
        ArrayList<String> data = new ArrayList<String>();
        data.add("Info");
        data.add("Assessment");
        data.add("Billing");
        data.add("Release");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
        GroupAdapter groupAdapter = new GroupAdapter(this,data);
        setListAdapter(groupAdapter);
        getListView().setDividerHeight(10);
    }

    void init(){
        colorList = ColorList.get();
    }

} 

我想在ListActivity中设置ListView的边距,但ListActivity.getListView中没有setMargin方法。

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

使用ViewGroup.MarginLayoutParams更改ListView的边距:

ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)
                                         getListView().getLayoutParams();
params.setMargins(left, top, right, bottom); 

答案 1 :(得分:0)

这是更新后的代码:

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    init();
    ArrayList<String> data = new ArrayList<String>();
    data.add("Info");
    data.add("Assessment");
    data.add("Billing");
    data.add("Release");
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
    GroupAdapter groupAdapter = new GroupAdapter(this,data);
    setListAdapter(groupAdapter);
    getListView().setDividerHeight(10);

    //setting the margin
    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) getListView().getLayoutParams();
    params.setMargins(10, 0, 10, 0);
}