Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android,如何获取/设置布局参数?_Android_Android Layout_Layoutparams - Fatal编程技术网

Android,如何获取/设置布局参数?

Android,如何获取/设置布局参数?,android,android-layout,layoutparams,Android,Android Layout,Layoutparams,在我的应用程序中,我有一个对话室。我所有的信息都应该向右对齐,而其余的则应该向左对齐 我的列表中的一行是这样的: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="

在我的应用程序中,我有一个对话室。我所有的信息都应该向右对齐,而其余的则应该向左对齐

我的列表中的一行是这样的:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/llContainer" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/speech_bubble_orange"
        android:id="@+id/llGroup" >

            <TextView
                android:id="@+id/message_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:shadowColor="@color/message_textShadow"
                android:shadowDx="1"
                android:shadowDy="1"
                android:text="Medium Text"
                android:textColor="@color/message_textColor"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/message_date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/temp_date"
                android:gravity="right"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"/>
    </LinearLayout>

</LinearLayout>
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        convertView = myInflater.inflate(R.layout.list_message_row, null);
        holder = new ViewHolder();

        holder.tvMessageBody = (TextView) convertView.findViewById(R.id.message_text);
        holder.tvMessageDate = (TextView) convertView.findViewById(R.id.message_date);
        holder.llContainer = (LinearLayout) convertView.findViewById(R.id.llContainer);
        holder.llGroup = (LinearLayout) convertView.findViewById(R.id.llGroup);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.tvMessageBody.setText(messageList.get(position).getMessageBody());
    String date = messageList.get(position).getSentDate();
    holder.tvMessageDate.setText(formatFanciedDate(date));

    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.llContainer.getLayoutParams();
    try {
        if(messageList.get(position).isMine()) {
            holder.llGroup.setBackgroundResource(R.drawable.speech_bubble_green);
            params.gravity = Gravity.RIGHT;
        }
        //If not mine then it is from sender to show orange background and align to left
        else {
            holder.llGroup.setBackgroundResource(R.drawable.speech_bubble_orange);
            params.gravity = Gravity.LEFT;
        }

        holder.llContainer.setLayoutParams(params);

    } catch (NullPointerException e) {
        e.printStackTrace();
    }

    return convertView;
}
我认为一切都应该很好,但是当我运行应用程序NullPointerException时,会发生异常并指向
params.gravity=gravity.RIGHT
params.gravity=gravity.LEFT(取决于isMine()方法)

你怎么想?我的错在哪里?
如果您有任何建议,我们将不胜感激。

请使用RelativeLayout参数并像这样设置重力,它将起作用

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
这在我的应用程序中适用。

getLayoutParams()
将返回
null
,直到视图附加到其父视图


尝试使用其构造函数创建布局参数,而不是使用
getLayoutParams()

谢谢Raja,你的意思是我将容器从LinearLayout更改为RelativeLayout?+1完全正确,非常感谢你在解决我的问题3小时后。谢谢;)