2016-11-16 53 views
1

我有一个RelativeLayout的内部的LinearLayout,我想将其调整到左边或右边取决于一个值(它是一个简单的聊天,左边是自己,右边是我是谁说话)的人,这就是布局代码:对准左/右一个的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); 

最近有没有ny linearLayout里面的相对一个和代码工作正常(我用一个TextView而不是LinearLayout),但我现在确实需要一个。

读取DOC,我认为我在访问父布局PARAMS,这就是为什么我把它转换为相对的,因为如果我改变线性我也有同样的异常。

如何设置对齐到的LinearLayout编程?

回答

0

在使用的LayoutParams父视图的LinearLayout代替RelativeLayout的。

使用

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

,而不是

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

但'LinearLayout.LayoutParams'does没有一个addRule添加'RelativeLayout.ALIGN_PARENT_RIGHT'property我所需要的。当然,我不能添加相对布局的属性。 –

+0

然后改变你的父视图的RelativeLayout – sasikumar

+0

父视图已经是一个RelativeLayout的。 –

1

试试这个:

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

它抛出一个异常:java.lang.ClassCastException:android.widget.LinearLayout $的LayoutParams不能转换到android.widget.RelativeLayout $的LayoutParams –

+0

如果你的父母布局相对再使用的,而不是LinearLayout.I的RelativeLayout已经编辑我的答案。 – Nidhi

0

试试这个:

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