以编程方式在RelativeLayout内右对齐/左对齐LinearLayout

时间:2016-11-16 09:20:58

标签: android android-layout

我在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),但我现在确实需要一个。

阅读文档,我想我正在访问父布局参数,这就是我将它转换为相对的原因,因为如果我改为Linear,我会有相同的异常。

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

3 个答案:

答案 0 :(得分:0)

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

使用

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

而不是

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

答案 1 :(得分:0)

试试这个:

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;
    }

答案 2 :(得分:0)

试试这个:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.pechen.Person;

public class Demo {
    public static void main(String [] args) throws Exception{
        String filename = "person.obj";            
        Person person = new Person();
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
        out.writeObject(person);
        out.close();

        Person p;
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
        p = (Person)in.readObject();
        in.close();
        System.out.println("Read Person: " + p.toString());
     }
}