0

我有以下布局,我想以编程方式设置边距。这个布局我在 baseadpter 中访问这个布局。我怎样才能访问它。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"    >



    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     >  

    <CheckBox
     android:layout_width="20dp"
     android:layout_height="17dp"
     android:button="@drawable/my_custom_checkbox"
     android:id="@+id/check"
     android:layout_marginTop="5dip"
     android:checked="false"
     >
    </CheckBox>

    <RelativeLayout
        android:id="@+id/Relative"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="6dip"
        android:layout_weight="0.5">

        <TextView android:id="@+id/list_item_entry_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:layout_marginTop="5dip"
            android:layout_marginBottom="15dip"
            android:ellipsize="marquee"
            android:text="Hello"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:fadingEdge="horizontal" />

  //Want to access this layout     
        <LinearLayout
        android:id="@+id/LinearCal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"    
        android:orientation="horizontal"
         android:layout_marginTop="50dip" to 100dip
4

3 回答 3

1

使用 findViewByID 方法。

http://developer.android.com/reference/android/view/View.html#findViewById(int)

然后访问它的布局参数

于 2013-05-15T10:39:47.410 回答
1

这是你的代码..

    LinearLayout ll = (LinearLayout) findViewbyid(R.id.LinearCal);

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    layoutParams.setMargins(0, 100, 0, 0);//set margin left, top, right, bottom

ll.setLayoutParams(layoutParams);
于 2013-05-15T10:40:22.983 回答
1

试试这个方法。

//LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(50, 10, 0, 0);

LinearLayout layout = (LinearLayout)findViewById(R.id.LinearCal);
layout.setLayoutParams(layoutParams);
于 2013-05-15T10:43:36.057 回答