表布局与Android Studio中的线性布局重叠

时间:2018-08-29 18:50:35

标签: android android-layout

我在按正确顺序排列内容时遇到问题。在模拟器中测试应用程序时,我发现表布局与线性布局重叠。我尝试了一些解决方案,但没有一个起作用。

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/table_background2" android:layout_width="match_parent" android:layout_height="match_parent">
    <RelativeLayout android:gravity="center" android:id="@+id/headerpicture" android:background="@color/loginheader" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true">
        <TextView android:textSize="20sp" android:textColor="#fff" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:text="@string/fixed_header"/>
        <ImageView android:id="@+id/imgLeftBack" android:layout_width="35dp" android:layout_height="35dp" android:src="@drawable/arrow_left_green" android:layout_alignParentRight="true" android:onClick="goBack" android:contentDescription="@string/empty"/>
    </RelativeLayout>
    <RelativeLayout android:gravity="center" android:id="@+id/footer" android:background="@color/loginheader" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true">


        <TextView android:textColor="#fff" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:text="@string/copyright" android:ems="8"/>
    </RelativeLayout>
    <TableLayout android:layout_gravity="top" android:id="@+id/tableLayout1" android:background="@drawable/gradient" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:layout_marginRight="5dp" android:layout_below="@+id/headerpicture">
        <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
            <TextView android:textColor="@color/list_item_text" android:ellipsize="none" android:layout_gravity="top" android:id="@+id/textViewUsernamePicture" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="@dimen/text_view_marginLeft" android:layout_marginTop="@dimen/text_view_marginTop" android:text="@string/pre_election_usernameLbl" android:layout_weight="1"/>
            <EditText android:layout_gravity="left|center_vertical|center_horizontal|center" android:id="@+id/txtUsernamePicture" android:layout_width="wrap_content" android:layout_height="@dimen/edit_text_height" android:layout_marginLeft="@dimen/text_view_marginLeft" android:ems="8" android:layout_weight="3" android:inputType="text"/>
        </TableRow>
        <TableRow android:layout_marginBottom="10dp" android:id="@+id/tableLayout2Row9" android:layout_width="wrap_content" android:layout_height="wrap_content">
            <EditText android:gravity="top|left|center_vertical|center_horizontal|center" android:layout_gravity="left|center_vertical|center_horizontal|center" android:id="@+id/txtTitle" android:layout_width="wrap_content" android:layout_height="wrap_content"  android:layout_weight="3" android:inputType="text" android:hint="Incident Title" android:layout_marginLeft="@dimen/table_marginLeft"/>
        </TableRow>
    </TableLayout>
    <ImageView android:layout_marginBottom="0dp" android:layout_gravity="center" android:id="@+id/ImgPicture" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/text_view_marginTop" android:src="@drawable/picture_placeholder" android:layout_below="@+id/tableLayout1" android:contentDescription="@string/empty"/>

    <TableLayout android:layout_gravity="top" android:id="@+id/tableLayout2" android:background="@drawable/gradient" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:layout_marginRight="5dp" android:layout_below="@+id/ImgPicture">

        <TableRow android:layout_marginBottom="10dp" android:id="@+id/tableLayout2Row13" android:layout_width="wrap_content" android:layout_height="wrap_content">
            <TextView android:textColor="@color/list_item_text" android:ellipsize="none" android:layout_gravity="top" android:id="@+id/textViewPriorityIncident" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="@dimen/text_view_marginLeft" android:layout_marginTop="@dimen/text_view_marginTop" android:text="@string/incident_priorityLbl" android:layout_weight="1"/>
            <Spinner android:entries="@array/incident_priority_arrays" android:id="@+id/spinnerPriorityIncident" android:layout_width="match_parent" android:layout_height="wrap_content" android:prompt="@string/incident_priority_prompt"/>
        </TableRow>

        <TableRow android:layout_marginBottom="10dp" android:id="@+id/tableLayout2Row5" android:layout_width="wrap_content" android:layout_height="wrap_content">
            <EditText android:gravity="top|left|center_vertical|center_horizontal|center" android:layout_gravity="left|center_vertical|center_horizontal|center" android:id="@+id/txtCommentsIncident" android:scrollbars="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/table_marginLeft" android:lines="4" android:minLines="3" android:ems="8" android:maxLength="@string/comments_length" android:layout_weight="3" android:inputType="textMultiLine" android:hint="Comment"/>

        </TableRow>
    </TableLayout>

    <LinearLayout android:id="@+id/btnsHolder" android:layout_gravity="center" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tableLayout2" android:layout_above="@id/footer">
        <Button android:background="@color/red" android:textColor="@color/redtextxolor"  android:tint="@color/redtextxolor" android:id="@+id/btnSaveImage" android:paddingLeft="@dimen/btn_padding_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="@string/btnBack" android:drawableLeft="@android:drawable/ic_media_previous" android:onClick="goBack"/>
         <Button  android:background="@color/btn" android:textColor="@color/btntextcolor"  android:tint="@color/btntextcolor"   android:layout_gravity="center" android:id="@+id/btnTakePicture" android:paddingLeft="10sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="@string/btnSnap" android:drawableLeft="@android:drawable/ic_menu_camera"  android:onClick="captureImage"/>
         <Button  android:background="@color/snap" android:textColor="@color/white"  android:tint="@color/white"   android:layout_gravity="center" android:id="@+id/btnSendPicture" android:paddingLeft="10sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="@string/btnSendSnap" android:drawableLeft="@android:drawable/ic_menu_send" android:onClick="sendForm"/>

     </LinearLayout>


</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

首先,这是我见过的线性布局中最糟糕的格式,只需将其设置为表格布局下即可

像这样

<TableLayout
    android:id="@+id/mytable"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</TableLayout>


<LinearLayout

    android:id="@+id/mylinear"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/mytable">
</LinearLayout>

注意android:layout_below =“ @ + id / mytable”>。我给表格视图提供的ID就是这样做,然后它们不应该重叠,现在在您的代码中,这意味着您将必须为表格布局提供一个ID

别在意高度和宽度,我给出了线性视图,如果剩下的layout_below只是我按下Enter键填充,您应该担心的一切

答案 1 :(得分:0)

<LinearLayout
 android:orientation="horizontal"
 android:background="@drawable/gradient"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginLeft="5dp"
 android:layout_marginTop="5dp"
 android:layout_marginRight="5dp" >
  <Spinner 
    android:entries="@array/incident_priority_arrays"
    android:id="@+id/spinnerPriorityIncident"
    android:layout_width="wrap_content"\ 
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:prompt="@string/incident_priority_prompt"/>
 <TextView 
    android:textColor="@color/list_item_text" 
    android:ellipsize="none"
    android:id="@+id/textViewPriorityIncident"
    android:layout_gravity="center_vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_marginLeft="@dimen/text_view_marginLeft" 
    android:layout_marginTop="@dimen/text_view_marginTop" 
    android:text="@string/incident_priorityLbl" />
</LinearLayout>


<LinearLayout 
    android:layout_marginBottom="10dp" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:orientation="vertical">
<EditText 
    android:gravity="center" 
    android:id="@+id/txtCommentsIncident"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/table_marginLeft" 
    android:lines="4" 
    android:minLines="3" 
    android:ems="8" 
    android:maxLength="@string/comments_length" 
    android:inputType="textMultiLine"
    android:hint="Comment"/>

</LinearLayout>

<LinearLayout android:id="@+id/btnsHolder"
    android:layout_gravity="center"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_below="@+id/tableLayout2"
    android:layout_above="@id/footer">
        <Button
        android:background="@color/red"
        android:textColor="@color/redtextxolor" 
        android:tint="@color/redtextxolor" 
        android:id="@+id/btnSaveImage"
        android:paddingLeft="@dimen/btn_padding_left" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="10dp"
        android:text="@string/btnBack"
        android:drawableLeft="@android:drawable/ic_media_previous"
        android:onClick="goBack"/>
         <Button 
         android:background="@color/btn"
         android:textColor="@color/btntextcolor"
         android:tint="@color/btntextcolor"
         android:layout_gravity="center" 
         android:id="@+id/btnTakePicture"
         android:paddingLeft="10sp"
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content"
         android:layout_margin="10dp"
         android:text="@string/btnSnap"
         android:drawableLeft="@android:drawable/ic_menu_camera" 
         android:onClick="captureImage"/>
         <Button 
         android:background="@color/snap"
         android:textColor="@color/white"
         android:tint="@color/white"  
         android:layout_gravity="center"
         android:id="@+id/btnSendPicture"
         android:paddingLeft="10sp" 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_margin="10dp"
         android:text="@string/btnSendSnap"
         android:drawableLeft="@android:drawable/ic_menu_send" 
         android:onClick="sendForm"/>

     </LinearLayout>