[英]ScrollView height doesn't fit to its content - Android Java
我有一個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);
謝謝你的幫助。
嘗試這個...
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);
}
}
我認為您指定了錯誤的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.