简体   繁体   English

ScrollView的高度不适合其内容-Android Java

[英]ScrollView height doesn't fit to its content - Android Java

I have a ScrollView and I want to add 2 images on it. 我有一个ScrollView,我想在上面添加2张图像。
I created a ScrollView and then a LinearLayout. 我创建了ScrollView,然后创建了LinearLayout。 Then created 2 ImageViews and added them on LinearLayout. 然后创建2个ImageViews并将它们添加到LinearLayout上。 Then added LinearLayout on ScrollView. 然后在ScrollView上添加LinearLayout。 (All of steps by java code. Didn't use XML at all) (所有步骤均由Java代码完成。根本不使用XML)
But the problem is the ScrollView height is not fitted to height of my images(It's too long). 但是问题是ScrollView的高度不适合我图像的高度(太长)。
This is my code: 这是我的代码:

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

Thanks for your help. 谢谢你的帮助。

Try this... 尝试这个...

activity_demo.xml 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 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);

    }

}

I think you specify wrong type of layoutParams, you should specify a layoutParam for the viewGroup which the child will be placed in. Try like this: 我认为您指定了错误的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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM