简体   繁体   English

如何在相对布局中居中表格布局

[英]how to center table layout inside relative layout

I have a table layout that contain two edit texts and a spinner in each row, I want to center it inside a relative layout but I couldn`t get around it. 我有一个表格布局,其中包含两个编辑文本和每行中的微调器,我想将其置于相对布局中但我无法绕过它。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
android:id="@+id/Relative_4"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:src="@drawable/SomePhoto" />

<TableLayout 
    android:layout_centerHorizontal="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="40dp"
    android:clickable="false"
    android:duplicateParentState="false"
    android:focusable="false"
    android:layoutDirection="rtl"
    android:focusableInTouchMode="false"
    android:longClickable="false"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:baselineAligned="true" >

    <EditText
        android:id="@+id/editText_4_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:duplicateParentState="false"
        android:ems="5"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:freezesText="false"
        android:gravity="center_vertical|center_horizontal"
        android:inputType="none"
        android:longClickable="false"
        android:scrollHorizontally="false"
        android:text="Course"
        android:textAllCaps="false" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText_4_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:duplicateParentState="false"
        android:ems="6"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center_vertical|center_horizontal"
        android:inputType="none"
        android:longClickable="false"
        android:text="credit hours"
        android:layout_centerHorizontal="true" />

    <EditText
        android:id="@+id/editText_4_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:duplicateParentState="false"
        android:ems="4"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center_vertical|center_horizontal"
        android:inputType="none"
        android:longClickable="false"
        android:text="Grades" />

</TableRow>

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="24.67" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >


<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/editText_4_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="4"
        android:gravity="center_vertical|center_horizontal"
        android:hint="course_1"
        android:maxLines="1" />

    <EditText
        android:id="@+id/editText_4_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="6"
        android:gravity="center_vertical|center_horizontal"
        android:inputType="number"
        android:text="0" />

    <Spinner
        android:id="@+id/spinner_4_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</TableRow>



<RelativeLayout
    android:id="@+id/tableRow13"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:baselineAligned="true" >



    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:background="@drawable/ResetBtn"
            android:id="@+id/button_4_1"
            android:layout_marginLeft="70dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            /> 
        <Button
        android:background="@drawable/CalculateBtn"
        android:id="@+id/button_4_2"
        android:layout_toRightOf="@+id/button_4_1"
        android:layout_marginLeft="90dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    </RelativeLayout>


</RelativeLayout>
</TableLayout>
</ScrollView>

<com.mopub.mobileads.MoPubView
android:id="@+id/adview"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
/>

</TableLayout>

</RelativeLayout>

Any help on how to get around this issue would be appreciated. 如何解决这个问题的任何帮助将不胜感激。 Thankx Thankx

Make these changes to you xml code..i hope it will work 对你进行这些修改xml代码..我希望它能工作

 <RelativeLayout 
    android:id="@+id/Relative_4"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" //This is new line
    >

    <TableLayout 
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_centerInParent="true"//This is new line      
        android:clickable="false"
        android:focusable="false"
        android:layoutDirection="rtl"
        android:focusableInTouchMode="false"
        android:longClickable="false"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

尝试在表格布局中添加此行:

 android:layout_centerInParent="true"
if you want whole table layout inside relative layout to center then do this :

<RelativeLayout 
android:id="@+id/Relative_4"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">

and if you want the content inside tablerow to center then you just add this line to your table row:
android:gravity="center"
 <TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM