LayoutInflater 不适用于 Android
LayoutInflater not working for Android
我正在尝试将 LinearLayout 作为子项添加到另一个 LinearLayout 组件,但它根本不会显示在屏幕上。
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ReceivedMessage"
android:background="@android:color/holo_blue_light"
android:visibility="visible"
tools:showIn="@layout/content_chat">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
android:id="@+id/ProfilePicture"
android:layout_weight="1" />
<TextView
android:text=" Message"
android:layout_width="305dp"
android:layout_height="match_parent"
android:id="@+id/MessageText"
android:gravity="center_vertical" />
</LinearLayout>
代码:
Button testMessageButton = (Button) findViewById(R.id.TestMessageButton);
testMessageButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
LinearLayout msgLayout = (LinearLayout) findViewById(R.id.MessageLayout);
LayoutInflater vi = (LayoutInflater)getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.message_received, msgLayout, false);
msgLayout.addView(v);
}
});
是什么导致组件没有出现在屏幕上?
你的问题不正确,我会用两种不同的方式回答
首先,如果您要添加的 LinearLayout 在不同的 xml 文件中,那么您可以扩充 xml 文件并将其添加到 Activity 中的父 LinearLayout class像这样
Button testMessageButton = (Button) findViewById(R.id.TestMessageButton);
testMessageButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
LinearLayout msgLayout = (LinearLayout) findViewById(R.id.MessageLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LayoutInflater vi = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.message_received, null);
v.setLayoutParams(params);
msgLayout.addView(v);
}
});
我已经更新了我的解决方案。部分问题出在您的布局文件中
这是Activityclass
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
private LinearLayout mainLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout = (LinearLayout)findViewById(R.id.main_layout);
Button addChatButton = (Button)findViewById(R.id.add_chat);
addChatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LayoutInflater vi = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout mView = (LinearLayout)vi.inflate(R.layout.test_layout, mainLayout, false);
mView.setLayoutParams(params);
mainLayout.addView(mView);
}
});
}
}
xml Activity class
的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.inducesmile.mylinear.MainActivity">
<Button
android:id="@+id/add_chat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="@string/add_chat"
android:layout_gravity="center|center_horizontal"/>
</LinearLayout>
最后,包含要扩充的 Linearlayout 的布局文件并添加到根 LinearLayout。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ReceivedMessage"
android:background="@android:color/holo_blue_light"
android:visibility="visible">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:id="@+id/ProfilePicture"
android:layout_weight="2" />
<TextView
android:text="@string/message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/MessageText"
android:layout_weight="7"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:gravity="center_vertical" />
</LinearLayout>
基于 如果将子容器添加到父容器有效,则可能会出现问题,因为父容器未放置在滚动视图中,因此您添加的所有子容器都将不可见(如果您愿意)确保已添加的视图只需在添加任何子项
后调用 parentlayout.getChildCount()
我正在尝试将 LinearLayout 作为子项添加到另一个 LinearLayout 组件,但它根本不会显示在屏幕上。
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ReceivedMessage"
android:background="@android:color/holo_blue_light"
android:visibility="visible"
tools:showIn="@layout/content_chat">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
android:id="@+id/ProfilePicture"
android:layout_weight="1" />
<TextView
android:text=" Message"
android:layout_width="305dp"
android:layout_height="match_parent"
android:id="@+id/MessageText"
android:gravity="center_vertical" />
</LinearLayout>
代码:
Button testMessageButton = (Button) findViewById(R.id.TestMessageButton);
testMessageButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
LinearLayout msgLayout = (LinearLayout) findViewById(R.id.MessageLayout);
LayoutInflater vi = (LayoutInflater)getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.message_received, msgLayout, false);
msgLayout.addView(v);
}
});
是什么导致组件没有出现在屏幕上?
你的问题不正确,我会用两种不同的方式回答
首先,如果您要添加的 LinearLayout 在不同的 xml 文件中,那么您可以扩充 xml 文件并将其添加到 Activity 中的父 LinearLayout class像这样
Button testMessageButton = (Button) findViewById(R.id.TestMessageButton);
testMessageButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
LinearLayout msgLayout = (LinearLayout) findViewById(R.id.MessageLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LayoutInflater vi = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.message_received, null);
v.setLayoutParams(params);
msgLayout.addView(v);
}
});
我已经更新了我的解决方案。部分问题出在您的布局文件中
这是Activityclass
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
private LinearLayout mainLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout = (LinearLayout)findViewById(R.id.main_layout);
Button addChatButton = (Button)findViewById(R.id.add_chat);
addChatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LayoutInflater vi = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout mView = (LinearLayout)vi.inflate(R.layout.test_layout, mainLayout, false);
mView.setLayoutParams(params);
mainLayout.addView(mView);
}
});
}
}
xml Activity class
的布局文件<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.inducesmile.mylinear.MainActivity">
<Button
android:id="@+id/add_chat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="@string/add_chat"
android:layout_gravity="center|center_horizontal"/>
</LinearLayout>
最后,包含要扩充的 Linearlayout 的布局文件并添加到根 LinearLayout。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ReceivedMessage"
android:background="@android:color/holo_blue_light"
android:visibility="visible">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:id="@+id/ProfilePicture"
android:layout_weight="2" />
<TextView
android:text="@string/message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/MessageText"
android:layout_weight="7"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:gravity="center_vertical" />
</LinearLayout>
基于