我有以下布局,我想以编程方式设置边距。这个布局我在baseadapter中访问这个布局。我该如何访问它。
<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
答案 0 :(得分:1)
使用findViewByID方法。
http://developer.android.com/reference/android/view/View.html#findViewById(int)
然后访问其layoutparams
答案 1 :(得分: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);
答案 2 :(得分: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);