Scrollview 不能只在相对布局中工作

Scrollview not working just in relative layout

我正在 android 中创建自定义视图(我想使用 Java 代码动态地进行)。我将相对布局扩展到 class。当我为此相对布局使用滚动视图时,它不起作用(不滚动,只显示相对布局中的按钮之一)。

但是当我将相对布局更改为线性布局并将线性布局方向设置为垂直时,它起作用了,我可以滚动布局。

这是自定义布局 class:

import android.content.Context;
import android.graphics.Color;
import android.widget.Button;
import android.widget.RelativeLayout;

import java.util.ArrayList;

public class CustomLayout extends RelativeLayout {

public static ArrayList<Button> btnList;
int btnHeight = pxFromDp(getContext(), 70);

public CustomLayout(Context context) {
    super(context);
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    setLayoutParams(params);

    //setOrientation(VERTICAL);  ---> for LinearLayout

    btnList = new ArrayList<>();
}

public void addButton(String buttonText, int id) {
    Button btn = new Button(getContext());

    if(id > 0)
        btn.setY(btnList.get(id - 1).getBottom());
    else
        btn.setY(0);

    btn.setText(buttonText);
    btn.setBackgroundColor(Color.GRAY);
    btnList.add(btn);
    addView(btnList.get(id), id,new LayoutParams(LayoutParams.MATCH_PARENT, btnHeight));
}

//just convert dpi to pixel
public static int pxFromDp(final Context context, float dp) {
    return (int) (dp * context.getResources().getDisplayMetrics().density);
    }
}

这是我对滚动视图 class 的用法:

import android.app.Activity;
import android.os.Bundle;
import android.widget.ScrollView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    CustomLayout csLayout = new CustomLayout(this);
    for (int i = 0 ; i < 20 ; i++) {
        csLayout.addButton("Button " + i, i);
    }

    ScrollView sv = new ScrollView(this);
    sv.addView(csLayout);

    setContentView(sv);
}
}

请记住,此自定义视图适用于扩展线性布局。

我也应该为 Relative Layout 做些什么?

因为 ScrollView 只能托管一个直接子布局。而且你附加了不止一个。最好将您的 Relativelayout 添加到 LinearLayout 更好的文档 Here

最好将 LinearLayout 和按钮添加到 Linearlayout,而不是将线性布局添加到 CustomLayout。喜欢

ScrollView sv = new ScrollView(this);
    sv.setFillViewport(true);
    CustomLayout customLayout = new CustomLayout(this);
    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setOrientation(LinearLayout.VERTICAL);

    for (int i = 0; i < 20; i++) {
        Button button = new Button(this);
        button.setText("Button" + i);
        linearLayout.addView(button);
    }
    customLayout.addView(linearLayout);
    sv.addView(customLayout);
    setContentView(sv);

但是您最好使用 xml 来准确地获得您的布局。

要在 RelativeLayout 中使用视图,您必须指定一些额外的属性,例如

android:layout_below="id"
android:layout_above ="id"
android:layout_toRightOf="id"
android:layout_toLeftOf="id"

视图在上方、下方和左侧(相对)。

正如您所说,如果在 LinearLayout 上它工作正常,因为您已经为该线性布局指定了垂直方向,这意味着无论何时向其添加任何元素,它都会自动位于最后一项下方添加。这就是为什么您可以使用 LinearLayout 看到所需的功能。

此外,正如@Devendra Singh 先前所建议的那样,您应该确保该 ScrollView 只有 1 个直接子级。

您的 RelativeLayout 中发生的事情是,绘制了所有按钮,但它们绘制在彼此之上。这就是为什么您只看到一个出现在屏幕上的原因。如果你想调试,尝试在你添加的按钮上设置文本,并根据for循环给它一个数字。您会看到显示的按钮是最后添加的按钮。

希望对您有所帮助...编码愉快!!!