尝试以编程方式设置线性布局的上边距

时间:2017-02-14 12:13:41

标签: android-linearlayout

我在滚动视图中有LinearLayout。我想使用下面的代码

以编程方式将LinearLayout的上边距设置为50dp
 convertView = inflater.inflate(R.layout.apps_detail_item, parent, false);
    LinearLayout childLayout = (LinearLayout)   convertView.findViewById(R.id.childLayout);
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
             LinearLayout.LayoutParams.WRAP_CONTENT,
             LinearLayout.LayoutParams.WRAP_CONTENT
     );
     params.setMargins(0, 50, 0, 0);
     childLayout.setLayoutParams(params);

但是当我尝试运行此代码时,我的应用程序正在崩溃。这是错误日志:

AndroidRuntime: FATAL EXCEPTION: main
Process: com.don.offers, PID: 11452
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
at android.widget.FrameLayout.onMeasure(FrameLayout.java:437)
at android.support.v4.widget.NestedScrollView.onMeasure(NestedScrollView.java:482)
at android.view.View.measure(View.java:17555)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17555)
at se.emilsjolander.flipview.FlipView.measureChild(FlipView.java:252)
at se.emilsjolander.flipview.FlipView.measureChildren(FlipView.java:245)
at se.emilsjolander.flipview.FlipView.onMeasure(FlipView.java:228)
at android.view.View.measure(View.java:17555)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
at android.view.View.measure(View.java:17555)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
at android.view.View.measure(View.java:17555)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:17555)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17555)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17555)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17555)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
at android.view.View.measure(View.java:17555)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2027)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1185)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1391)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1073)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5903)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:773)
at android.view.Choreographer.doCallbacks(Choreographer.java:586)
at android.view.Choreographer.doFrame(Choreographer.java:556)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:759)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5268)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)

这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_color"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottomBar"
        android:scrollbars="none">

        <LinearLayout
            android:id="@+id/childLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="200dp">

                <ImageView
                    android:id="@+id/banner_image"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:adjustViewBounds="true"
                    android:scaleType="fitCenter"
                    android:src="@drawable/default_image_300_200" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:background="@drawable/top_tint" />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:layout_alignParentBottom="true"
                    android:background="@drawable/bottom_tint">

                    <!--<de.hdodenhof.circleimageview.CircleImageView
                        android:id="@+id/logo"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="10dp"
                        android:src="@drawable/ic_launcher"
                        app:border_color="#FFF"
                        app:border_overlay="true"
                        app:border_width="2dp" />-->
                    <ImageView
                        android:id="@+id/logo"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="10dp"
                        android:scaleType="fitCenter"
                        android:src="@drawable/ic_launcher" />

                    <TextView
                        android:id="@+id/headLine"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="15dp"
                        android:layout_toLeftOf="@+id/dislike_btn"
                        android:layout_toRightOf="@+id/logo"
                        android:ellipsize="end"
                        android:singleLine="true"
                        android:textAppearance="@android:style/TextAppearance"
                        android:textColor="@color/white" />
                </RelativeLayout>

            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/ratingbar_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#ffffff"
                    android:orientation="horizontal"
                    android:padding="10dp">

                    <LinearLayout
                        android:id="@+id/people_used_count_layout"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_alignParentLeft="true"
                        android:layout_toLeftOf="@+id/verified_date_txt_layout"
                        android:layout_weight=".4"
                        android:gravity="center_vertical">

                        <RatingBar
                            android:id="@+id/ratingBar"
                            style="?android:attr/ratingBarStyleSmall"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:isIndicator="true"
                            android:numStars="5"
                            android:rating="3.5" />


                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/verified_date_txt_layout"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_weight=".6"
                        android:gravity="center_vertical|right">

                        <TextView
                            android:id="@+id/downloadAndEarn"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:ellipsize="end"
                            android:singleLine="true"
                            android:text="@string/download_use_earn_text"
                            android:textAppearance="@android:style/TextAppearance"
                            android:textColor="@color/text_color_dark_gray"
                            android:textSize="@dimen/fourteen_sp_text_size" />

                        <TextView
                            android:id="@+id/downloadAndEarnValue"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginLeft="5dp"
                            android:text="&#x20B9;20"
                            android:textAppearance="@android:style/TextAppearance"
                            android:textColor="@color/green"
                            android:textSize="@dimen/twentifour_sp_text_size"
                            android:textStyle="bold"
                            android:visibility="gone"/>
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/price_discount_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="10dp">

                    <TextView
                        android:id="@+id/appName"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:textAppearance="@android:style/TextAppearance"
                        android:textColor="@color/text_color_dark_gray"
                        android:textSize="@dimen/sixteen_sp_text_size" />

                    <TextView
                        android:id="@+id/appDescription"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:textAppearance="@android:style/TextAppearance"
                        android:textColor="@color/text_color_dark_gray"
                        android:textSize="@dimen/fourteen_sp_text_size" />

                </LinearLayout>


                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="5dp"
                    android:background="@color/divider_color" />

                <LinearLayout
                    android:id="@+id/user_goals_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:orientation="vertical">


                </LinearLayout>


            </LinearLayout>

        </LinearLayout>

    </ScrollView>

    <RelativeLayout
        android:id="@+id/bottomBar"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:background="@drawable/button_background">


        <TextView
            android:id="@+id/installNow"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/install_now_text"
            android:textAppearance="@android:style/TextAppearance"
            android:textColor="@color/white"
            android:textSize="@dimen/sixteen_sp_text_size" />


    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/app_progress"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#33000000"
        android:visibility="gone"
        >
        <!--<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:text="@string/please_wait"
            />-->
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            />
    </RelativeLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

我自己解决了。这是代码

^[1-9][0-9]*$