【问题标题】:Relative layout android loopable相对布局android loopable
【发布时间】:2015-11-24 22:26:36
【问题描述】:

我正在编写一个应用程序,它为找到的每个文件加载一排按钮(2 个按钮,然后是下面的黑线)(因此循环计数不会是静态的)。目前,在构建时,我的静态循环计数为 15。但是在运行代码时,它会在左侧创建较大的按钮,并且在下方创建黑线……但是……右侧的较小按钮仅出现一次。知道为什么吗?

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ScrollView scrollPictures = new ScrollView(this);
    RelativeLayout appLayout = new RelativeLayout(this);
   // appLayout.setClipBounds(null);
    Resources r = getResources();
    ImageView blackLine;

    RelativeLayout.LayoutParams p;
    int id = 1;
    for(int x = 1; x <= 15; x++){
        p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        Button studentsButton = new Button(this);
        studentsButton.setClipBounds(null);
        studentsButton.setId(id);
        studentsButton.setHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics()));
        studentsButton.setWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 700, r.getDisplayMetrics()));
        //studentsButton.setBackgroundColor(Color.LTGRAY);
        if (x > 1 ){
            p.addRule(RelativeLayout.BELOW, id - 1);
            studentsButton.setLayoutParams(p);
        }

        appLayout.addView(studentsButton);
        id ++;

        p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        Button soundButton = new Button(this);
        soundButton.setClipBounds(null);
        soundButton.setHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics()));
        soundButton.setWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics()));
        //soundButton.setBackgroundColor(Color.LTGRAY);
        p.addRule(RelativeLayout.RIGHT_OF, id - 1);
        soundButton.setLayoutParams(p);
        appLayout.addView(soundButton);

        p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        blackLine = new ImageView(this);
        blackLine.setId(id);
        blackLine.setBackgroundColor(Color.BLACK);
        blackLine.setMinimumWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 700, r.getDisplayMetrics()));
        blackLine.setMinimumHeight(3);
        blackLine.setMinimumWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 700, r.getDisplayMetrics()));
        blackLine.setClipBounds(null);
        p.addRule(RelativeLayout.BELOW, id - 1);
        blackLine.setLayoutParams(p);
        appLayout.addView(blackLine);
        id++;


    }

    scrollPictures.addView(appLayout);
    setContentView(scrollPictures);

}

【问题讨论】:

    标签: java android layout screen


    【解决方案1】:

    老实说,在代码中完成所有这些似乎有点过头了。我建议创建一个布局文件,并多次膨胀。将 appLayout 设为垂直方向的 LinearLayout,就这么简单:

    for(int x = 1; x <= 15; x++){
      View view = LayoutInflater.from(this).inflate(R.layout.some_layout, appLayout, false);
    
      // Configure the items inside the view
    
      Button button1 = (Button)view.findViewById(R.id.button1);
      button1.setText("Button 1");
      button1.setOnClickListener(new View.OnClickListener() { ... });
    
      ...
    
      appLayout.addView(view); 
    }
    

    而且你的布局文件也会相对简单:

    <LinearLayout android:orientation="vertical" ...>
      <LinearLayout android:orientation="horizontal" ...>
        <Button android:id="@+id/button1" ... />
        <Button android:id="@+id/button2" ... />
      </LinearLayout>
      <View android:background="@android:color/black"
            android:layout_height="3dp"
            android:layout_width="match_parent" />
    </LinearLayout>
    

    虽然在代码中布局元素绝对是可行的,但正确处理可能会让人头疼。我发现使用 XML 文件更容易,并像这样根据需要对它们进行扩充。我的猜测是您使用 id 的方式会混淆布局。最好让按钮获得自己的 id(不要使用 setId),并在设置相对布局参数时使用“getId”方法。

    【讨论】:

    • 我尝试按照您的方式进行操作,但“视图”的定义会导致错误。一开始,“getContext()”并不是一个可以解析的方法。然后,如果我使用“getApplicationContext()”会导致错误; LayoutInflater 中的 from() 不能应用于......有什么想法吗?
    • 这取决于您从哪里调用它。如果您正在参加活动,请使用“this”
    • 我得到与之前相同的错误“无法将 LayoutInflater 中的 from() 应用于”......。必需:上下文,找到:MainActivity,LinearLayout,布尔值,原因:实际和正式参数列表的长度不同
    • 对不起,错过了一个括号。修复它。
    • 要做"inflate(R.id.some_layout....." 你需要给一个布局的id...我添加了 - android:id="@+id/some_layout" - 作为测试 - schemas.android.com/apk/res/android" - 在代码中它一直说它期望布局不是 ID...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 2013-11-23
    • 2014-02-14
    相关资源
    最近更新 更多