2016-01-21 66 views
1

我想膨胀AsyncTask的onPostExectue方法上的布局,但没有显示,我认为我的视图膨胀,因为我的父ScrollView的大小增加。但为什么什么都看不见?为什么Android布局充气器膨胀空白?

下面是我在做什么在onPostExecute方法:

LinearLayout rootView = (LinearLayout) getActivity().findViewById(R.id.root_view); 
View child = getActivity() 
       .getLayoutInflater() 
       .inflate(R.layout.layout_child, rootView, false); 
rootView.addView(child); 

子布局进行充气:

<?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:gravity="center_vertical" 
    android:orientation="horizontal"> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="20dp" 
     android:background="@null" 
     android:src="@drawable/ic_play_arrow_black_24dp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="T 1" 
     android:textSize="30sp" /> 

</LinearLayout> 

父布局,其中我不断膨胀:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.abc.DetailFragment" 
    tools:showIn="@layout/activity_a"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:id="@+id/root_view"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <ImageView 
       android:id="@+id/poster" 
       android:layout_width="139dp" 
       android:layout_height="209dp" 
       android:layout_margin="10dp" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_margin="10dp" 
       android:orientation="vertical" 
       android:padding="5dp"> 

       . 
       . 
       . 

      </LinearLayout> 

     </LinearLayout> 

     <TextView 
      android:id="@+id/overview" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textSize="20sp" 
      android:layout_margin="8dp" /> 

    </LinearLayout> 
</ScrollView> 

我没有错误,但只是一些空白。

回答

2

首先,我认为您应该将活动的观点扩大到活动,但为了您目前的工作方式,请尝试在runOnUiThread中充气并添加一个孩子。

runOnUiThread(new Runnable() { 
     public void run() { 
     LinearLayout rootView = (LinearLayout) getActivity().findViewById(R.id.root_view); 
     View child = getActivity() 
         .getLayoutInflater() 
         .inflate(R.layout.layout_child, rootView, false); 
     rootView.addView(child); 
    } 
}); 

另外,如果你知道你的根视图是LinearLayout,为什么不使用的,与其View