В моето приложение използвам фрагменти (табове) и когато се опитам да задам границата на LinearLayout на оформлението на фрагмента, това не работи.
Моето оформление има този RelativeLayout
...
<RelativeLayout
android:id="@+id/calendar_activities"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp">
</RelativeLayout>
...
Моят фрагмент:
public class FragmentTabSchedule extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmenttab1, container, false);
RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.calendar_activities);
LinearLayout ll = new LinearLayout(inflater.getContext());
ll.setOrientation(LinearLayout.VERTICAL);
//ll.setPadding(60, 60, 60, 60);
Resources r = inflater.getContext().getResources();
int h = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
60,
r.getDisplayMetrics()
);
LayoutParams params = new LayoutParams(
LayoutParams.MATCH_PARENT,
h
);
params.setMargins(60, 60, 60, 60);
ll.setLayoutParams(params);
ll.setBackgroundColor(getResources().getColor(R.color.yellow));
TextView act = new TextView(inflater.getContext());
act.setText("Activity test");
ll.addView(act);
rl.addView(ll);
return view;
}
}
Linearlayout е създаден правилно, единственото нещо, което не работи, е маржът (вече се опита с подпълване без успех). Оформлението е поставено с поле 0.
Когато се опитам да създам кода директно в xml файла, той работи
<RelativeLayout
android:id="@+id/calendar_activities"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:layout_marginTop="120dp"
android:background="@color/blue">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"/>
</LinearLayout>
</RelativeLayout>