Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式将一个相对布局放置在另一个相对布局旁边_Android - Fatal编程技术网

Android 以编程方式将一个相对布局放置在另一个相对布局旁边

Android 以编程方式将一个相对布局放置在另一个相对布局旁边,android,Android,我有两个相对的布局,目前显示一个低于另一个。我希望第二个布局以编程方式显示在第一个相对布局的右侧 下面是我使用的代码 main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_hei

我有两个相对的布局,目前显示一个低于另一个。我希望第二个布局以编程方式显示在第一个相对布局的右侧

下面是我使用的代码

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <RelativeLayout 
         android:layout_width="620dp"
         android:layout_height="wrap_content"
         android:orientation="vertical"
         android:id="@+id/lay1" >    
         <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/hello1" />
    </RelativeLayout>
    <RelativeLayout 
         android:layout_width="620dp"
         android:layout_height="wrap_content"
         android:orientation="vertical" 
         android:id="@+id/lay2">  
         <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/hello2" />
    </RelativeLayout>
</LinearLayout>

提前感谢

将线性布局方向更改为水平。我还将线性布局的权重和设置为2,并将每个相对布局的权重设置为1,以确保平均分割。

修改代码中的这些更改,您将获得水平方向

在main.xml中

android:layout\u width=“620dp”
to
android:layout\u width=“wrap\u content”

LinearLayout

在myAct.java中

public class myAct extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout layoutContainer = new LinearLayout(this);
        layoutContainer.setLayoutParams(new     LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        // Arguments here: width, height, weight 
        LinearLayout.LayoutParams childLp = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
        RelativeLayout layoutLeft = new RelativeLayout(this);

        layoutContainer.addView(layoutLeft, childLp);
        RelativeLayout layoutRight = new RelativeLayout(this);
        layoutContainer.addView(layoutRight, childLp);
     }
}
删除
setContentView
之后的所有语句并添加以下行

LinearLayout ll = (LinearLayout)findViewById(R.id.ll);
ll.setOrientation(LinearLayout.HORIZONTAL);

如果有任何疑问,请尝试一下。

看看本文中编写的谓词或行布局代码:

根据您的要求更改以下行

child.layout(xpos, ypos, xpos + childw, ypos + childh);
例如,如果一行中只需要两个文本视图,请执行以下操作

if((i%2) != 0)

xpos = screenWidth / 2;

else

xpos = 0;

@金丝看来很傻。为什么在标题中添加Programmaticaly?:-)现在我有n个相对布局,我想用两个布局排成一行。有人能帮我吗。。。