需要帮助编辑相对布局

时间:2016-04-12 22:43:13

标签: android android-relativelayout

我是android studio的新手,并且建立了一个小计算器。这是一个截图:

http://i.stack.imgur.com/Q76Oo.jpg

正如您所看到的,它与左边界对齐,而不是与我喜欢的中心对齐。 所有按钮都基于√按钮。这是坏事吗?我如何让它们居中?

由于

编辑 - 这是我的content_only代码:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.thegreat.douglas.calculator.OnlyActivity"
    tools:showIn="@layout/activity_only">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sqrt_key"
        android:id="@+id/sqrt_key"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="178dp"
        android:onClick="operator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/seven_key"
        android:id="@+id/seven_key"
        android:layout_below="@+id/sqrt_key"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/four_key"
        android:id="@+id/four_key"
        android:layout_below="@+id/seven_key"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/one_key"
        android:id="@+id/one_key"
        android:layout_below="@+id/four_key"
        android:layout_alignRight="@+id/four_key"
        android:layout_alignEnd="@+id/four_key"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/zero_key"
        android:id="@+id/zero_key"
        android:layout_below="@+id/one_key"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/point_key"
        android:id="@+id/point_key"
        android:layout_below="@+id/one_key"
        android:layout_toRightOf="@+id/one_key"
        android:layout_toEndOf="@+id/one_key"
        android:onClick="operator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/two_key"
        android:id="@+id/two_key"
        android:layout_above="@+id/zero_key"
        android:layout_toRightOf="@+id/one_key"
        android:layout_toEndOf="@+id/one_key"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/five_key"
        android:id="@+id/five_key"
        android:layout_above="@+id/one_key"
        android:layout_toRightOf="@+id/four_key"
        android:layout_toEndOf="@+id/four_key"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/eight_key"
        android:id="@+id/eight_key"
        android:layout_above="@+id/four_key"
        android:layout_toRightOf="@+id/seven_key"
        android:layout_toEndOf="@+id/seven_key"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/left_parenth_key"
        android:id="@+id/left_parenth_key"
        android:layout_above="@+id/seven_key"
        android:layout_toRightOf="@+id/sqrt_key"
        android:layout_toEndOf="@+id/sqrt_key"
        android:onClick="operator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/right_parenth_key"
        android:id="@+id/right_parenth_key"
        android:layout_above="@+id/eight_key"
        android:layout_toRightOf="@+id/eight_key"
        android:layout_toEndOf="@+id/eight_key"
        android:onClick="operator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/nine_key"
        android:id="@+id/nine_key"
        android:layout_below="@+id/left_parenth_key"
        android:layout_toRightOf="@+id/eight_key"
        android:layout_toEndOf="@+id/eight_key"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/six_key"
        android:id="@+id/six_key"
        android:layout_below="@+id/eight_key"
        android:layout_toRightOf="@+id/five_key"
        android:layout_toEndOf="@+id/five_key"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/three_key"
        android:id="@+id/three_key"
        android:layout_above="@+id/point_key"
        android:layout_toRightOf="@+id/point_key"
        android:layout_toEndOf="@+id/point_key"
        android:onClick="numbers" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/negative_key"
        android:id="@+id/negative_key"
        android:layout_alignBottom="@+id/point_key"
        android:layout_toRightOf="@+id/point_key"
        android:layout_toEndOf="@+id/point_key"
        android:onClick="specialOperator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/multiply_key"
        android:id="@+id/multiply_key"
        android:layout_above="@+id/nine_key"
        android:layout_toRightOf="@+id/right_parenth_key"
        android:layout_toEndOf="@+id/right_parenth_key"
        android:onClick="operator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/subtract_key"
        android:id="@+id/subtract_key"
        android:layout_above="@+id/six_key"
        android:layout_toRightOf="@+id/nine_key"
        android:layout_toEndOf="@+id/nine_key"
        android:onClick="operator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add_key"
        android:id="@+id/add_key"
        android:layout_above="@+id/three_key"
        android:layout_toRightOf="@+id/six_key"
        android:layout_toEndOf="@+id/six_key"
        android:onClick="operator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/enter_key"
        android:id="@+id/enter_key"
        android:layout_toRightOf="@+id/three_key"
        android:layout_toEndOf="@+id/three_key"
        android:layout_alignBottom="@+id/negative_key"
        android:layout_alignTop="@+id/three_key"
        android:onClick="enterKey" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/clear_key"
        android:id="@+id/clear_key"
        android:onClick="clearEquation"
        android:layout_above="@+id/left_parenth_key"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text=""
        android:id="@+id/equation"
        android:layout_above="@+id/clear_key"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/multiply_key"
        android:layout_alignEnd="@+id/multiply_key"
        android:clickable="false"
        android:textAlignment="textEnd"
        android:height="@dimen/abc_action_bar_stacked_max_height" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/square_key"
        android:id="@+id/square_key"
        android:layout_below="@+id/equation"
        android:layout_toRightOf="@+id/sqrt_key"
        android:layout_toEndOf="@+id/sqrt_key"
        android:onClick="specialOperator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/negative_exp_key"
        android:id="@+id/neg_exp_key"
        android:layout_below="@+id/equation"
        android:layout_toRightOf="@+id/left_parenth_key"
        android:layout_toEndOf="@+id/left_parenth_key"
        android:onClick="specialOperator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/divide_key"
        android:id="@+id/divide_key"
        android:layout_below="@+id/equation"
        android:layout_toRightOf="@+id/right_parenth_key"
        android:layout_toEndOf="@+id/right_parenth_key"
        android:onClick="operator" />


</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

Can you Share the xml-layout design code than we can help you:

**Edited:**
 Use `android:gravity="center"` code in Root node of the xml file.

**In your case:**

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.thegreat.douglas.calculator.OnlyActivity"
    tools:showIn="@layout/activity_only">
<Button ...
// Add all button design code here

</RelativeLayout>

答案 1 :(得分:1)

使用 android:layout_gravity =&#34; center&#34; 。并为父布局的 dimen 文件夹中的填充管理空格。     你的要求应该完成。