0

我想要一個的LinearLayout添加到scrolview 這是我的代碼 代碼編譯,但它並沒有告訴我新的佈局添加的LinearLayout當前視圖不工作

這是原始佈局(即我想補充的話)

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/ScrollView01" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:scrollbars="vertical" 

android:layout_margin="15dp" 
android:layout_marginTop="15dp"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="10dp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:id="@+id/ViewHistoryImageLayout"> 

    <ImageView 
     android:id="@+id/HistoryImage" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="0.76" 
     android:gravity="center" 
     android:padding="10dp" 
     android:src="@drawable/upload" 
     android:contentDescription="@string/HistoryImage"/> 

    <TextView 
     android:id="@+id/TranslatedText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.12" 
     android:paddingBottom="10dp" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp" 
     android:text="@string/translateImageButton" /> 

</LinearLayout> 

,這是我想補充幾次佈局:

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

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<RatingBar 
    android:id="@+id/ratingBar1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:numStars="5" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="TextView" /> 

</LinearLayout> 

,並添加新的佈局的Java代碼:

setContentView(R.layout.activity_view_history_image); 
ScrollView sv = new ScrollView(this); 
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View ll = inflater.inflate(R.layout.translation_menu, null); 
sv.addView(ll); 

代碼編譯的罰款,並在應用程序正在運行,但沒有任何反應 有與.xml文件的一個問題?

TNX

+0

現在您將scrollview佈局添加到動態創建的滾動視圖。這肯定會失敗。 – ePeace

回答

0

我認爲你需要改變你的layout_width在原始LinearLayoutwrap_content而不是match_parent。它佔用了整個佈局

+0

我試過了,它不起作用 –

1

您應該檢索現有的ScrollView佈局而不是創建它。

setContentView(R.layout.activity_view_history_image); 
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View ll = inflater.inflate(R.layout.translation_menu, null); 
ScrollView sv = (ScrollView) findViewById(R.id.ScrollView01); 
sv.addView(ll); 

可能會有所幫助。

+0

add(sv)給誰? –

+0

tnx,但我得到的exeption:scrollview只能託管直接子 –

+0

解決了它: 我需要將新的linearlayout添加到scrollview內的linearLayout。 工作代碼: LinearLayout sv =(LinearLayout)findViewById(R.id.ViewHistoryImageLayout); sv.addView(ll); –

0

我有同樣的問題。 我試圖在onSizeChange上做到這一點,並沒有奏效。 所以這段代碼幫助我:

 layout.postDelayed(new Runnable() { 
      @Override 
      public void run() { 

       LinearLayout ll = new LinearLayout(getContext()); 

       ll.setOrientation(LinearLayout.HORIZONTAL); 

       ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(200, ViewGroup.LayoutParams.MATCH_PARENT); 

       layout.addView(ll, params); 
      } 
     }, 100); 

佈局是滾動型內的LinearLayout。

0

我建立不同的ImageView做它的工作原理,這樣的:

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.selected_spotphotos_container); 
     imageView = new ImageView(getApplicationContext()); 
     imageView1 = new ImageView(getApplicationContext()); 
     imageView2 = new ImageView(getApplicationContext()); 

     imageView.setImageBitmap(bmImg1); 
     imageView1.setImageBitmap(bmImg2); 
     imageView2.setImageBitmap(bmImg3); 

     linearLayout.addView(imageView,0); 
     linearLayout.addView(imageView1,1); 
     linearLayout.addView(imageView2,2); 

的XML代碼是這一個:

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="1" 
    > 
.... 

<HorizontalScrollView 
     android:id="@+id/hori_scroll_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="false" 
     android:foregroundGravity="left" 
     android:paddingBottom="1dp" 
     android:paddingTop="1dp" 
     android:visibility="visible" 
     > 

     <LinearLayout 
      android:id="@+id/selected_spotphotos_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="left" 
      android:animateLayoutChanges="true" 
      android:gravity="left" 
      android:orientation="horizontal" 
      android:visibility="visible" 
      > 

      <!-- images will be added dynamicall to this layout --> 
     </LinearLayout> 
    </HorizontalScrollView> 
//here u can add different EditText or Button or whatever you want 
</LinearLayout> 

我希望這可以幫助別人。

相關問題