我有一个ScrollView,我想在其上添加2个图像。
我创建了一个ScrollView,然后创建了一个LinearLayout。然后创建了2个ImageViews并将它们添加到LinearLayout上。然后在ScrollView上添加了LinearLayout。 (java代码的所有步骤。根本没有使用XML)
但问题是ScrollView高度不适合我的图像高度(它太长了)。
这是我的代码:
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 :(得分: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);
}
}
答案 1 :(得分: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);