【问题标题】:Scrollview not working just in relative layout滚动视图不能仅在相对布局中工作
【发布时间】:2016-08-17 01:44:19
【问题描述】:

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

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

这是自定义布局类:

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);
    }
}

这是我在 Scroll View 中对此类的使用:

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);
}
}

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

我也应该如何为相对布局工作?

【问题讨论】:

    标签: android android-layout layout android-linearlayout android-relativelayout


    【解决方案1】:

    要在 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 循环给它一个数字。您将看到显示的按钮是最后添加的按钮。

    我希望这会有所帮助...祝您编码愉快!!!

    【讨论】:

      【解决方案2】:

      因为ScrollView 只能承载一个直接子布局。而且您要附加多个。最好将您的 Relativelayout 添加到 LinearLayout 更好的文档Here

      最好将LinearLayout 和按钮添加到Linearlayout,而不是将线性布局添加到您的CustomLayout.Like

      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 来准确地获取布局。

      【讨论】:

      • 相对布局本身是否不止一个孩子?
      • 但是当我在 XML 中执行此操作(将相对布局添加到滚动视图)时,它可以正常工作。
      • 不,但是当您添加相对布局 20 次时。所以20个孩子补充道。您可以在相对布局中添加 20 个 20 按钮,然后尝试。
      • 正如您在我的代码中看到的那样,我没有添加 20 次相对布局。我添加了 20 次按钮。如果你认为我添加了 20 次相对布局,那么现在我应该在代码中写什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多