2010-09-21 68 views
1

这是一个android开发问题。
如果我首先使用setContentView(R.layout.main);将内容视图设置为我的xml布局,然后使用addContentView(layout, params)添加另一个内容视图,则内容视图会相互重叠。我希望第二个内容视图直接在第一个视图下定位。是否有可能或者我需要以某种方式结合xml和编程创建的布局?两种布局都是线性的。彼此之间的布局

在此先感谢

回答

5

把一个LinearLayout内部布局都与垂直方向。而已。

编辑:

我的意思是,你必须创建一个3布局的XML文件。

  1. main.xml中
  2. layout1.xml - >你的第一个布局
  3. layout2.xml - >你的第二个布局

主布局文件,建议立即进行删除是这样的:

<LinearLayout android:orientation="vertical"> 
<include android:id="+id/lay1" android:layout="@layout/layout1"/> 
<include android:id="+id/lay2" android:layout="@layout/layout2"/> 
</LinearLayout> 

现在您的主要布局到您的setContentView(R.layout.main)

+0

对不起,让您烦恼,但是如何将xml布局转换为LinearLayout呢? – 2010-09-21 09:42:42

+0

@Mountain King:你必须使用'include'标签。我在我的帖子中做了一个修改。查看。 – Praveen 2010-09-21 10:18:35

1

谢谢你们。我很欣赏这些反馈。

无法使父布局(CoordinatorLayout)上的方向垂直。

虽然将两个元素都放在垂直方向的LinearLayout中,

上面的一条评论说我应该首先制作一个appbarlayout:是的,我改变了它。我已将其更改为LinearLayout,作为我尝试让项目停止彼此绘制的一种尝试。

我不确定这是解决这个问题的正确方法,但它工作。我会继续前进。再次感谢。

0

要放在对方(一个顶部下除外)顶部的布局,这就是我所做的:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


<RelativeLayout 
    android:id="@+id/topLayout" 
    android:layout_width="match_parent" 
    android:layout_height="300dp" 
    android:layout_alignParentTop="true" 
    android:background="@color/colorAccent"> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="126dp" 
     android:text="Existing Client" 
     android:textColor="@android:color/white" 
     android:textSize="20sp" 
     android:textStyle="bold" /> 
</RelativeLayout> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="300dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:background="@color/colorPrimary"> 

    <TextView 
     android:id="@+id/textView7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="138dp" 
     android:text="New Client" 
     android:textColor="@android:color/white" 
     android:textSize="20sp" 
     android:textStyle="bold" /> 

</RelativeLayout> 

而结果:

enter image description here

我的意图是使用布局作为填充屏幕的一定宽度的按钮。