【问题标题】:Android use RelativeLayout ProgrammaticallyAndroid以编程方式使用RelativeLayout
【发布时间】:2014-04-22 00:35:25
【问题描述】:

我有一个基本的应用程序正在运行,但现在我专注于格式化应用程序,并且遇到了我想要的东西布局的困难。

我有一个图像列表,用户可以使用下一个按钮从一个图像切换到下一个图像。图像和下一个按钮都以编程方式添加到页面(我清除布局中的任何内容,然后添加 ImageView 和 Button)。现在,我不是将它们放在另一个之上,而是尝试将它们并排放置,因此图像将占据大部分空间,然后下一个按钮将位于其右侧。

查看文档,我倾向于使用 RelativeLayout 来完成此操作。但是,我在以编程方式使用 RelativeLayouts 时遇到了一些问题。

Activity.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">


    <RelativeLayout
        android:orientation="vertical"
        android:id="@+id/activity_training_package_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context="com.example.xxx.PackageActivity"
        tools:ignore="MergeRootFrame">
        </RelativeLayout>
</ScrollView>

尝试以编程方式添加按钮:

public void addNextButton(final int currentFile, final RelativeLayout layout) {
    Button next = new Button(this);
    next.setWidth(100);

    int id = layout.getChildAt(0).getId(); // the image is the only thing there

RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

lay.addRule(RelativeLayout.RIGHT_OF, id);
next.setLayoutParams(lay);
    next.setText("NEXT >>");
    next.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            showNextFile(currentFile, layout);
        }
    //layout.setLayoutParams(lay);
    layout.addView(next);
...

我只是想知道我应该为此设置哪个 LayoutParams,布局的 LayoutParams 还是视图的?当我尝试将其设置为布局时,我得到一个强制转换异常(由于某种原因,它需要 FrameLayout.LayoutParams,而不是 RelativeLayout.LayoutParams)。

有人可以指出正确的方向来弄清楚如何使用布局吗?我似乎找不到解释我应该设置哪些 LayoutParams 的资源。

TL;DR 如何以编程方式使用 RelativeLayouts 和 LayoutParams?

【问题讨论】:

标签: android android-layout


【解决方案1】:

最简单的解决方案是将HorizontalSrollViewLinearLayout 结合使用。

activity_test.xml

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <LinearLayout
    android:id="@+id/container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" />

</HorizontalScrollView>

TestActivity.java

public class TestActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    LinearLayout container = (LinearLayout)findViewById(R.id.container);

    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    String next = getResources().getString(R.string.next);

    for(int i=0;i<5;i++){

       ImageView imgView = new ImageView(mActivity);
       imgView.setLayoutParams(params);
       imgView.setImageResource(R.drawable.photo);
       container.addView(imgView);

       Button button = new Button(mActivity);
       button.setLayoutParams(params);
       button.setText(next);
       container.addView(button);

    }
  }
}

【讨论】:

  • 感谢您的提示,但这并没有真正解决我的问题 - 按钮仍会出现在图片下方
  • 其实没有。我们正在使用 Horizo​​ntalLayout。 Horizo​​ntalLayout 的子级从左到右水平显示。这是 Nexus 7 (2013) 上的结果截图。 TestActivity.
  • 嗨撕。您是否测试过解决方案?如果是这样,请您接受答案吗?
  • 我已经测试了您的解决方案,但是我无法让它为我工作 - 正如我在之前的评论中所说,它正在将它们放在彼此之上
  • 汤姆,你能发个截图吗?你用的是什么设备?
【解决方案2】:

在您的 XML 文件中设置线性布局权重属性并为图像视图和按钮分配权重

main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:weightSum="1" /> 

MainActivity.java

package com.example.stackoverflowdemos;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TableLayout;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout container = (LinearLayout)findViewById(R.id.container);
        //Dynamically generate imageview and button
        ImageView imgView = new ImageView(this);
           imgView.setLayoutParams(new TableLayout.LayoutParams(0, LayoutParams.FILL_PARENT, .2f)); //set imageview height and width using weight
           imgView.setImageResource(R.drawable.ic_launcher);
           container.addView(imgView);

           Button button = new Button(this);
           button.setLayoutParams(new TableLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, .8f)); //set Button height and width using weight
           button.setText("next");
           container.addView(button);

     }

}

【讨论】:

    猜你喜欢
    • 2011-04-28
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多