2015-06-24 51 views
1

我有一個ScrollView,我想在其上添加2個圖像。
我創建了一個ScrollView,然後是一個LinearLayout。然後創建2個ImageView並將它們添加到LinearLayout中。然後在ScrollView上添加LinearLayout。 (所有的步驟由Java代碼。根本沒有使用XML)
但問題是ScrollView高度不適合我的圖像的高度(它太長了)。
這是我的代碼:ScrollView高度不適合其內容 - Android Java

LinearLayout scrollParent = new LinearLayout(this); 
    scrollParent.setOrientation(LinearLayout.VERTICAL); 
    scrollParent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

    helpView = new ScrollView(this); 
    helpView.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT)); 
    helpView.setFillViewport(true); 

    inside = new LinearLayout(this); 
    inside.setOrientation(LinearLayout.VERTICAL); 
    inside.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT)); 

    helpView.addView(inside); 

    ImageView img = new ImageView(this); 
    img.setImageResource(R.drawable.learn1); 
    img.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT)); 



    ImageView img2 = new ImageView(this); 
    img2.setImageResource(R.drawable.learn2); 
    img2.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT)); 


    inside.addView(img); 
    inside.addView(img2); 
    scrollParent.addView(helpView); 

感謝您的幫助。

+0

使用ScrollView.LayoutParams,而不是試圖在第2行 – HeisenBerg

+0

RelativeLayout.LayoutParams,沒有工作@HeisenBerg – SAKT

+0

嘗試LinearLayout.LayoutParams.MATCH_PARENT代替ViewGroup.LayoutParams.MATCH_PARENT – HeisenBerg

回答

0

嘗試......

activity_demo.xml

<LinearLayout 
    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" 
    android:orientation="vertical" 
    android:id="@+id/myLinear" 
    tools:context="com.example.ricardo.demo.Demo"> 


</LinearLayout> 

Demo.java

package com.example.ricardo.demo; 

import android.app.Activity; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.ScrollView; 


public class Demo extends Activity { 

    private ScrollView helpView = null; 
    private LinearLayout inside = null, myLinear = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_demo); 

     myLinear = (LinearLayout) findViewById(R.id.myLinear); 
     showImages(); 
    } 

    private void showImages(){ 
     helpView = new ScrollView(this); 
     helpView.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT)); 
     helpView.setFillViewport(true); 

     inside = new LinearLayout(this); 
     inside.setOrientation(LinearLayout.VERTICAL); 
     inside.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT)); 

     helpView.addView(inside); 

     ImageView img = new ImageView(this); 
     img.setImageResource(R.drawable.image1); 
     img.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT)); 
     img.setScaleType(ImageView.ScaleType.FIT_XY); 
     inside.addView(img); 

     ImageView img2 = new ImageView(this); 
     img2.setImageResource(R.drawable.image2); 
     img2.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT)); 
     img2.setScaleType(ImageView.ScaleType.FIT_XY); 
     inside.addView(img2); 

     myLinear.addView(helpView); 

    } 

} 
+0

有沒有命名的事「 WRAP_PARENT「,所以我改變了它。它根本沒有幫助。 – SAKT

+0

對不起,是WRAP_CONTENT,使用ctrl +空格尋求IDE的幫助 – Woz

+0

是的,我做到了,並將其改爲WRAP_CONTENT,但沒有幫助。 @Woz – SAKT

0

我想你指定錯誤類型的LayoutParams的,你應該指定一個的ViewGroup layoutParam這孩子將被放置在嘗試這樣的:

// I don't know which type of layout is the parent of scrollParent, 
// but you should set its layoutParams as well, from your code 
// i assume that is LinearLayout 
LinearLayout scrollParent = new LinearLayout(this); 
scrollParent.setOrientation(LinearLayout.VERTICAL); 
scrollParent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

helpView = new ScrollView(this); 
helpView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT)); 
helpView.setFillViewport(true); 

inside = new LinearLayout(this); 
inside.setOrientation(LinearLayout.VERTICAL); 
inside.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, 
      ScrollView.LayoutParams.WRAP_CONTENT)); 

helpView.addView(inside); 

ImageView img = new ImageView(this); 
img.setImageResource(R.drawable.learn1); 
img.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT)); 


ImageView img2 = new ImageView(this); 
img2.setImageResource(R.drawable.learn2); 
img2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT)); 

inside.addView(img); 
inside.addView(img2); 
scrollParent.addView(helpView); 
+0

感謝您的幫助,我編輯了該文件,但結果沒有改變。現在你提到它,我還有一個問題,根據你所說的,當一個LinearLayout是child並且RelativeLayout是parent時,它應該是這樣的:linear.setLayoutParams(new RelativeLayout.layoutParams(X,Y)); 。什麼應該是X和Y? LinearLayout.layoutParams.MATCH_PARENT或RelativeLayout.layoutParams.MATCH_PARENT? @yahya – SAKT

+0

Oopps。是的,這也應該是父母的:)我忘了更改ScrollView.LayoutParams的值,請修改上面我編輯的代碼,然後再試一次。 – yahya