Проблема: Я хочу прояснить очень простой вопрос с макетом. Я был у него 2 дня безрезультатно. Я просто хочу успешно установить RelativeLayout внутри LinearLayout и контролировать его позиционирование. Для этой цели я создал 2 макета: 1 макет, который будет отображаться слева и один справа.Ошибка компоновки: Вложение RelativeLayout внутри LinearLayout
Я получаю следующий вывод: Вы можете видеть, что второй вид должен отображаться справа, но его нет. Я попробовал layout_gravity, но не работал.
Ниже приведены макеты: Для слева:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/message_view_border">
<!--The message text-->
<TextView
android:id="@+id/message_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"/>
<!--the timestamp-->
<TextView
android:id="@+id/time_stamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/message_content"
android:textStyle="bold"/>
</RelativeLayout>
для правостороннего:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="@drawable/message_view_border"
android:layout_gravity="right"
android:gravity="right"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<!--The message text-->
<TextView
android:id="@+id/message_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"/>
<!--the timestamp-->
<TextView
android:id="@+id/time_stamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/message_content"
android:textStyle="bold"/>
</RelativeLayout>
Вот активность, которая работает сценарий:
package com.example.testapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Activity1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
//Get handle to the root view's child. i.e, FrameLayout
ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
LayoutInflater inflater = LayoutInflater.from(this);
LinearLayout linearLayout = (LinearLayout) viewGroup.findViewById(R.id.container_linear_layout);
View message_receiver = inflater.inflate(R.layout.layout_chat_message_receiver, viewGroup, false);
TextView received_content = (TextView)message_receiver.findViewById(R.id.message_content);
received_content.setText("okay");
linearLayout.addView(message_receiver);
View message_sender = inflater.inflate(R.layout.layout_chat_message_sender, viewGroup, false);
TextView sent_content = (TextView)message_sender.findViewById(R.id.message_content);
sent_content.setText("okay");
linearLayout.addView(message_sender);
}
}
Проект для справки: https://github.com/mankum93/TestApp1
Примечание: Я знаю, что я задавал подобный вопрос, без ответа на него. Я спрашиваю об этом снова, потому что люди задавались вопросом в вопросе о RecyclerView, который я использовал для заполнения динамического списка представлений. Возможно, это и было причиной того, что никто не мог определить основную причину проблемы. Но поскольку проблема является фундаментальной, которая существует даже без RecyclerView, я думаю, она заслуживает еще одного шанса.
Я искал несколько форумов, документов и сообщений SO, но не могу решить эту проблему.
EDIT 1: я могу видеть источник путаницы здесь: Здесь основной файл макета деятельности:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/container_linear_layout">
</LinearLayout>
<!--Floating view to send messages layout-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<EditText
android:id="@+id/typed_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SEND"
android:layout_gravity="bottom"
android:layout_weight="0"/>
</LinearLayout>
</FrameLayout>
Нет необходимости использовать ненормативную лексику в ваших скриншотах. Это довольно непрофессионально. –
@ Tanis.7x Извините. Ред. –
Какая ориентация у вашего LinearLayout есть и есть ли причина, по которой вы добавляете макеты в Java вместо xml? – codeMagic