nil*_*ash 1 android margins relativelayout
嗨,我正在开发Android应用程序,其中我正在创建相对布局编程并尝试设置边距并将其添加到具有方向线性的线性布局.所以这是我的代码:
<RelativeLayout 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"
android:background="@color/screen_background"
tools:context=".ChooseChannelsFragment" >
<LinearLayout
android:id="@+id/main_outer_llt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
</LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
在内部片段我正在添加这样的相对布局
RelativeLayout relativeLayout = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(200, 80);
relativeParams.setMargins(20, 20, 20, 20);
relativeLayout.setLayoutParams(relativeParams);
relativeLayout.setBackgroundColor(getResources().getColor(R.color.green_color));
linearLayout.addView(relativeLayout);
Run Code Online (Sandbox Code Playgroud)
它使用给定的颜色和大小创建布局,但不接受边距.难道我做错了什么?这该怎么做?需要帮忙.谢谢.
您在视图上使用的LayoutParams类型实际上应来自其父级.
因此,如果您将RelativeLayout添加到LinearLayout,则设置为RelativeLayout的LayoutParams实际上应该是LinearLayout.LayourParams,而不是RelativeLayout.LayoutParams.
| 归档时间: |
|
| 查看次数: |
9877 次 |
| 最近记录: |