2016-10-14 80 views
0

我想將LinearLayout作爲子項添加到另一個LinearLayout組件,但它不會顯示在屏幕上。LayoutInflater不適用於Android

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

是什麼原因造成的組件不會出現在屏幕上?

+0

你只想添加一個線性佈局到另一個線性佈局?那麼充氣機是什麼?爲什麼你用這個? –

+0

因爲父LinearLayout是一個垂直的,並且只用於排序。水平LinearLayout是我想要插入的,它包含一個圖像和文本。我使用了充氣器,因爲您無法克隆LinearLayout。 – ashjack

回答

0

你的問題是不正確的,但我會回答它以兩種不同的方式

首先,如果你想添加的LinearLayout是在不同的XML文件,那麼你就可以充氣的XML文件,並把它添加到父的LinearLayout在你的Activity類這樣

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

我已經更新了我的解決方案。問題的部分原因是在你的佈局文件

這是Activity類

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

的Activity類的XML佈局文件

<?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> 

enter image description here

+0

謝謝你的回答。我的情況是第一種情況,我嘗試使用代碼,但該組件仍然不顯示在LinearLayout中。 – ashjack

+0

添加mLinearLayout.setOrientation(LinearLayout.HORIZONTAL);你有沒有在你的logcat中的錯誤? – Inducesmile

+0

我已經添加了該功能,但仍然沒有運氣。 Logcat也沒有顯示任何錯誤。 – ashjack

0

基於Inducesmile如果加上孩子的父容器的作品則因爲父容器沒有放在一個滾動視圖裏面,所以你要添加不會對你可見的所有孩子的問題可能發生如果你想確認已經添加的視圖,只需在添加任何子項後調用parentlayout.getChildCount()

相關問題