【问题标题】:Android: Margin left and right not working together programmaticallyAndroid:左右边距不能以编程方式协同工作
【发布时间】:2016-10-11 01:22:55
【问题描述】:

我尝试以编程方式在我的 relativeLayout 中添加边距。我已经编写了代码(如下所述)。但是当我运行它时,只有左边距正常工作(右边距不工作)。

我正在使用以下代码:

 public static void setMargins (View v, int l, int t, int r, int b) {
    if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
        p.setMargins(l, t, r, b);
        v.requestLayout();
    }
}

像这样设置边距:

setMargins(mainLayout,100,0,100,0);

这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/contact_include_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <include layout="@layout/view_common_header_with_tittle" />
    </LinearLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
    <RelativeLayout
        android:id="@+id/mainLayout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="@dimen/u_done_action_bottom_margin"
            android:layout_marginRight="@dimen/u_common_margin_left"
            android:layout_marginLeft="@dimen/u_common_margin_left"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="@dimen/u_widget_height"
                android:id="@+id/showCountryDialog"
                android:background="@drawable/edittext_input_background_not_focus"
                android:layout_marginTop="@dimen/u_card_button_margin_botton">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Choose Country"
                    android:id="@+id/CountryDialogDefaultText"
                    android:textColor="#4d4d4d"
                    android:textSize="@dimen/u_common_text_size"
                    android:layout_centerVertical="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    />

                <ImageView
                    android:id="@+id/u_register_third_step_country_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/ic_menu_downarrow_grey"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="@dimen/u_common_text_size"
                    android:visibility="gone"
                    android:layout_alignParentRight="true"
                    />
            </RelativeLayout>


            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/u_common_margin_left"
                android:orientation="horizontal"

                >

                <TextView
                    android:id="@+id/u_call_center_t1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#4d4d4d"
                    android:textSize="@dimen/u_common_text_size"
                    android:text="@string/u_call_center_phone" />

                <TextView
                    android:id="@+id/u_call_center_phone"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="10dp"
                    android:textColor="#4d4d4d"
                    android:textSize="@dimen/u_common_text_size"
                     />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginTop="@dimen/u_spinner_margin_top"
                >

                <TextView
                    android:id="@+id/u_call_center_t2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#4d4d4d"
                    android:textSize="@dimen/u_common_text_size"
                    android:text="@string/u_call_center_wh" />

                <TextView
                    android:id="@+id/u_call_center_wh"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="10dp"
                    android:textColor="#4d4d4d"
                    android:textSize="@dimen/u_common_text_size" />
            </LinearLayout>




        </LinearLayout>


    </RelativeLayout>
    </ScrollView>
</LinearLayout>

我怎么解决这个问题?谢谢大家

【问题讨论】:

  • 在代码中的setMargins(mainLayout,100,0,100,0); 后面添加以下行:yourlayout.setLayoutParams(params);

标签: android margin android-xml android-relativelayout


【解决方案1】:

你应该传递密度像素(dp)值而不是像素(px)使用这个函数:

public static int dpToPx(int dp) {
    return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}

并像这样在设置边距函数中调用它:

 setMargins(mainLayout,dpToPx(100,0,dpToPx(100),0);

对于相对布局,使用这种方式以编程方式设置边距:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(0, dpToPx(10), 0, 0);
                v.setLayoutParams(params);
                v.requestLayout();

【讨论】:

  • java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams 无法转换为 android.widget.RelativeLayout$LayoutParams @Kapil Rajput
【解决方案2】:

您应该尝试marginStart 和marginEnd。当 marginLeft 和 marginRight 不起作用时,它对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-05
    • 2014-05-04
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 2017-11-21
    • 2011-05-27
    相关资源
    最近更新 更多