вертикальная полоса прокрутки Android и содержимое не видно - PullRequest
0 голосов
/ 09 августа 2011

Я создал простой вид, который отображает кнопки одинаковой ширины и высоты, и они соответствуют ширине экрана.

Когда я использую этот вид внутри линейного макета, он показывает очень хорошо.

Мне нужно, чтобы это было внутри вертикальной компоновки, поэтому, когда кнопки превышают высоту экрана, я хочу иметь возможность прокрутки, чтобы увидеть больше кнопок.

Однако, размещение этого представления внутри ScrollView делает мой виджет / представление невидимым. Что-то в моей раскладке не так, наверное. Пожалуйста, помогите.

Вот мой взгляд:

package com.uberaktiv.ui.layout;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class MetroLikeLayout extends ViewGroup {

    private static final int PADDING_TO_EDGES = 2;
    private static final int PADDING_BETWEEN_ITEMS = 4;

    private int viewWidth = 0;
    private int childControlWidth = 0;
    private int childControlHeight = 0;
    private int numberOfGridRowItems = 2;

    public MetroLikeLayout(Context context) {
        super(context, null);
    }

    public MetroLikeLayout(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
    }

    public MetroLikeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setNumberOfGridRowItems(int numGridRowItems) {
        this.numberOfGridRowItems = numGridRowItems;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
        viewWidth = parentWidth;
        childControlWidth = (viewWidth - (PADDING_TO_EDGES * 2) - (PADDING_BETWEEN_ITEMS * (numberOfGridRowItems - 1))) / numberOfGridRowItems;
        childControlHeight = childControlWidth;
        this.setMeasuredDimension(parentWidth, parentHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int row = 0;
        int col = 0;
        for (int i = 0; i < getChildCount(); i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() == GONE) {
                continue;
            }
            if ((i % numberOfGridRowItems) == 0) {
                row++;
                col = 1;
            }
            int left = PADDING_TO_EDGES + ((col - 1) * (childControlWidth + PADDING_BETWEEN_ITEMS));
            int top = PADDING_TO_EDGES + ((row - 1) * (childControlHeight + PADDING_BETWEEN_ITEMS));
            child.layout(left, top, left + childControlWidth, top + childControlHeight);
            col++;
        }
    }
}

Вот мой макет:

<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ubergrid="http://schemas.android.com/apk/res/com.uberaktiv.ui.layout.MetroLikeLayout">
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="above the table" android:layout_weight="0.0" />
    <ScrollView android:layout_width="match_parent" android:id="@+id/scrollView1" android:layout_height="fill_parent" android:layout_weight="1.0">
        <com.uberaktiv.ui.layout.MetroLikeLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
            <Button android:text="Button 1" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
            <Button android:text="Button 2" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
            <Button android:text="Button 3" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
            <Button android:text="Button 4" android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
            <Button android:text="Button 5" android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        </com.uberaktiv.ui.layout.MetroLikeLayout>
    </ScrollView>
</LinearLayout>

Основной вид деятельности самый простой:

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Пожалуйста, помогите, потому что я расстроен и не знаю, что я делаю неправильно. Как я уже писал выше ... если вы замените представление прокрутки другим линейным макетом или даже просто разместите пользовательский вид сразу после текстового виджета, все это будет работать.

Ждем вашей помощи и извините за большой кусок кода и тестов.

Моше

1 Ответ

0 голосов
/ 09 августа 2011

Попробуйте поместить ScrollView внутри MetroLikeLayout вокруг только кнопок, как это ...

<com.uberaktiv.ui.layout.MetroLikeLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
    <ScrollView android:layout_width="match_parent" android:id="@+id/scrollView1" android:layout_height="fill_parent" android:layout_weight="1.0">
        <Button android:text="Button 1" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="Button 2" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="Button 3" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="Button 4" android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="Button 5" android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    </ScrollView>
</com.uberaktiv.ui.layout.MetroLikeLayout>
...