Android: Cómo aplicar LayoutParams a mi RelativeLayout

I'm having difficult with this bit of code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    RelativeLayout layout = (RelativeLayout)findViewById(R.id.RelativeLayout1);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

I have my RelativeLayout1 in xml like this:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingLeft="60dp"
    android:paddingTop="53dp" >

But when I apply the scrollview, the fill_parent for RelativeLayout1 does not work as it should when it is not a child of scrollview. When it is not a child (i.e. I remove scrollview completely) then the relative layout covers the whole screen as I like it to. But when I put it inside scrollview, it only reaches the last element (a button) inside the relativelayout even though I use fill_parent.

So I am trying it programmatically to see if it makes a difference, but I can't seem to figure out how to apply my params variable to my layout one in the first bit of code. If you have any other ideas, do let me know as well.

preguntado el 31 de julio de 12 a las 14:07

3 Respuestas

Has olvidado llamar al método setter layout.setLayoutParams(params). To apply your created parameters you have to give them to the actual layout.

Tenga cuidado con el uso de fill_parent cuando el padre es ScrollView. Como la vista de desplazamiento adapta su tamaño a sus hijos.

Respondido 31 Jul 12, 14:07

I figured out how to solve my problem by adding a field to the scrollview and not doing this programmatically. In the ScrollView, I had to set the following:

android:fillViewport="true"

Respondido 31 Jul 12, 14:07

Agregar el atributo android:fillViewport="true" to your ScrollView if you want the RelativeLayout to fill up the ScrollView.

See Romain Guy's post about the same here: ScrollView's handy trick

Respondido 31 Jul 12, 14:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.