【问题标题】:Aligning to right/left a LinearLayout inside a RelativeLayout programmatically以编程方式在RelativeLayout中向右/向左对齐LinearLayout
【发布时间】:2016-11-16 17:52:37
【问题描述】:

我在 RelativeLayout 中有一个 LinearLayout,我想根据一个值将它向右或向左对齐(这是一个简单的聊天,左边是自己的,右边是我正在与之交谈的人),这是布局代码:

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/layout_chat_message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/chat_margin_default"
                android:paddingRight="@dimen/chat_margin_default"
                android:layout_alignParentRight="true"
                android:background="@drawable/textview_rounded_corners_receiver"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textview_chat_message_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textIsSelectable="true"
                    android:autoLink="all"
                    android:text="Se"/>

                <TextView
                    android:id="@+id/textview_chat_message_date_info"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textIsSelectable="false"
                    android:textAlignment="textEnd"
                    android:text="10:32"
                    android:maxLines="1"
                    android:textSize="@dimen/chat_message_text_font_size"/>

            </LinearLayout>


        </RelativeLayout>

如您所见,android:layout_alignParentRight="true" 属性已设置。

问题是,当我尝试以编程方式访问 LayoutParams 以将它们设置为向右或向左时,它会引发转换异常:

java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

这是我访问 LayoutParams 的方式:

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mChatMessageContainer.getLayoutParams();
            params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

mChatMessageContainer 在哪里:

        mChatMessageContainer = (View) v.findViewById(R.id.layout_chat_message);

最近,相对的内部没有任何 linearLayout 并且代码工作正常(我使用 TextView 而不是 LinearLayout),但我现在确实需要一个。

阅读文档时,我想我正在访问父布局参数,这就是我将其转换为相对的原因,因为如果我更改为线性,我也会遇到同样的异常。

如何以编程方式将对齐方式设置为 THAT linearLayout?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    在 LayoutParams 中使用父视图 LinearLayout 而不是 RelativeLayout。

    使用

    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.mChatMessageContainer.getLayoutParams();
    

    而不是

     RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mChatMessageContainer.getLayoutParams();
    

    【讨论】:

    • 但是“LinearLayout.LayoutParams”没有添加规则来添加我需要的“RelativeLayout.ALIGN_PARENT_RIGHT”属性。当然,我不能添加相对布局的属性。
    • 然后将您的父视图更改为RelativeLayout
    【解决方案2】:

    试试这个:

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
              params.setMargins(50,0,0,0);
              params.gravity = Gravity.RIGHT;
              holder.comment_layout.setLayoutParams(params);
              holder.comment_layout.setPadding(dipToPixels(mContext, 5), dipToPixels(mContext, 5), dipToPixels(mContext, 18), dipToPixels(mContext, 5));
    

    dipToPixels 方法:

    public static int dipToPixels(Context context, float dipValue) {
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
            int px =(int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, metrics));
    
            return  px;
        }
    

    【讨论】:

    • 抛出异常:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
    • 如果你的父布局是相对的,那么使用 RelativeLayout 而不是 LinearLayout。我已经编辑了我的答案。
    【解决方案3】:

    试试这个:

    RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        relativeParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT //or left ,depends your value);
        holder.mChatMessageContainer.getChildAt(0).setLayoutParams(relativeParams);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-17
      • 2020-09-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      • 2019-01-16
      相关资源
      最近更新 更多