I tried setting the weight, but this just makes the ScrollView
disappear.
Anyway, I have figured out the fix: Use a RelativeLayout for the main
view instead of LinearLayout. See code below.
This was a useful reference: http://www.anddev.org/viewtopic.php?p=11159
package com.test.ScrollViewTest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class BlogArticleView extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// the main layout of the screen
RelativeLayout rl = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("First line of text");
tv1.setId(1); // Important to set ID, otherwise getId doesn't
work!!!...
// ...See http://www.anddev.org/viewtopic.php?p=11159
{ // use this enclosure to prevent accidentally using params1 on
another element
RelativeLayout.LayoutParams params1 = new
RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rl.addView(tv1, params1);
}
TextView tv2 = new TextView(this);
tv2.setText("Second line of text");
tv2.setId(2);
{ // use this enclosure to prevent accidentally using params2 on
another element
RelativeLayout.LayoutParams params2 = new
RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.BELOW, tv1.getId());
rl.addView(tv2, params2);
}
ScrollView sv = new ScrollView(this);
LinearLayout ll2 = new LinearLayout(this);
ll2.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll2);
// Add some stuff to scrollView to test its depth
// (it should leave a margin at bottom of screen)
for (int i = 1; i < 10; i++)
{
TextView textView = new TextView(this);
textView.setText("Text View " + i);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll2.addView(textView, p);
Button buttonView = new Button(this);
buttonView.setText("Button " + i);
ll2.addView(buttonView, p);
}
{ // use this enclosure to prevent accidentally using params3 on
another element
RelativeLayout.LayoutParams params3 = new
RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT); //
Fill down to stretch
space
params3.addRule(RelativeLayout.BELOW, tv2.getId());
params3.bottomMargin = 50; // Add 50 pixel margin to ScrollView
rl.addView(sv, params3);
}
Button b = new Button(this);
b.setText("Button anchored to bottom");
{ // use this enclosure to prevent accidentally using params4 on
another element
RelativeLayout.LayoutParams params4 = new
RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rl.addView(b, params4);
}
setContentView(rl);
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en