Android:设置ALIGN_PARENT_RIGHT不起作用

Mar*_*eiz 3 layout android alignment

我尝试创建一个应用程序,用户可以为大型设备设置"单手布局",只用一只手输入数字.(看看我的意思)

在此输入图像描述

当我按下"L"时,布局向左转.这很有效.但在此之后,当我按下L时,布局不会移动.我不明白为什么......你能解释一下我做错了吗?

按钮"L":

Button cmd_change = (Button) findViewById(R.id.cmd_changesite);

cmd_change.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Log.v("TEST: ", ""+shellpreference.getInt("CHANGESITE", 0));
        if(sharedpreference.getInt("CHANGESITE", 0) == 0){

            RelativeLayout relativeLayout= (RelativeLayout)findViewById(R.id.rl_changesite);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();

            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            relativeLayout.setLayoutParams(params);
            sharedpreference.setInt("CHANGESITE", 1);
        }else{
            RelativeLayout relativeLayout= (RelativeLayout)findViewById(R.id.rl_changesite);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();

            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            relativeLayout.setLayoutParams(params);
            sharedpreference.setInt("CHANGESITE", 0);
        }

    }
}); 
Run Code Online (Sandbox Code Playgroud)

布局XML:

<?xml version="1.0" encoding="utf-8"?>
<!-- Einstellung des Gesamtlayouts -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="5dp"
    android:background="@drawable/bg1200x800"
    android:orientation="vertical" >

    <!-- Anzeige txt_standardsaetze -->


    <!-- Einbindung Scoll Funktion -->

    <RelativeLayout
        android:id="@+id/rl_betraege"
        android:layout_width="wrap_content"
        android:layout_height="180dp"
        android:layout_alignParentLeft="true" >

        <RelativeLayout
            android:id="@+id/relativeLayout5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="10dp" >

            <RelativeLayout
                android:id="@+id/relativeLayout4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp" >

                <TextView
                    android:id="@+id/txt_netto_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Netto"
                    android:textSize="18dip" />

                <TextView
                    android:id="@+id/TextView05"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:text=".-"
                    android:textSize="18dip" />

                <TextView
                    android:id="@+id/txt_nettobetrag"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_toLeftOf="@+id/TextView05"
                    android:text="0.00"
                    android:textSize="18dip" />
            </RelativeLayout>

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/relativeLayout4"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="3dp"
                android:background="@drawable/line"
                android:src="@drawable/line" />

            <RelativeLayout
                android:id="@+id/RelativeLayout02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/imageView2"
                android:layout_marginTop="10dp" >

                <TextView
                    android:id="@+id/txt_steuer_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:text="Steuer"
                    android:textSize="18dip" />

                <TextView
                    android:id="@+id/TextView07"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="10dp"
                    android:text=".-"
                    android:textSize="18dip" />

                <TextView
                    android:id="@+id/txt_steuerbetrag"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_toLeftOf="@+id/TextView07"
                    android:text="0.00"
                    android:textSize="18dip" />
            </RelativeLayout>

            <ImageView
                android:id="@+id/ImageView01"
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/RelativeLayout02"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="3dp"
                android:background="@drawable/line"
                android:src="@drawable/line" />

            <RelativeLayout
                android:id="@+id/RelativeLayout03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/ImageView01"
                android:layout_marginTop="10dp" >

                <TextView
                    android:id="@+id/txt_bruttodesc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:text="Brutto"
                    android:textSize="18dip"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/TextView10"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="10dp"
                    android:text=".-"
                    android:textSize="18dip"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/txt_bruttobetrag"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_toLeftOf="@+id/TextView10"
                    android:text="0.00"
                    android:textSize="18dip"
                    android:textStyle="bold" />
            </RelativeLayout>

            <ImageView
                android:id="@+id/ImageView02"
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/RelativeLayout03"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="3dp"
                android:background="@drawable/line"
                android:src="@drawable/line" />

            <ImageView
                android:id="@+id/ImageView04"
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:layout_below="@+id/ImageView02"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="1dp"
                android:background="@drawable/line"
                android:src="@drawable/line" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/relativeLayout5"
            android:layout_alignParentLeft="true" >

            <ImageView
                android:id="@+id/ImageView03"
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:layout_alignParentBottom="true"
                android:background="@drawable/line"
                android:src="@drawable/line" />

            <TextView
                android:id="@+id/company"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:text="Eingabe:"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/TextView03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:gravity="right"
                android:text=".-"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/edit_betrag"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/company"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@+id/TextView03"
                android:gravity="right"
                android:text="-"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </RelativeLayout>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_steuern"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/rl_betraege"
        android:layout_alignRight="@+id/rl_betraege"
        android:layout_below="@+id/rl_betraege"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp" >

        <Spinner
            android:id="@+id/spinner_steuer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@+id/TextView01" />

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="Steuer:"
            android:textSize="18dip" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_eingabeart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/rl_steuern"
        android:layout_alignRight="@+id/rl_steuern"
        android:layout_below="@+id/rl_steuern" >

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="Eingabe:"
            android:textSize="18dip" />

        <RadioGroup
            android:id="@+id/radiogrp_netbrut"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@+id/TextView02"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radiobutton_netto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="@string/netto_calc_"
                android:textSize="18dip" />

            <RadioButton
                android:id="@+id/radiobutton_brutto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/brutto_calc_"
                android:textSize="18dip" />
        </RadioGroup>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_parent_changesite"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/rl_eingabeart"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" >

        <RelativeLayout
            android:id="@+id/rl_changesite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/cmd_1"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_alignBaseline="@+id/cmd_2"
                android:layout_alignBottom="@+id/cmd_2"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:layout_toLeftOf="@+id/cmd_2"
                android:background="@drawable/zahlen"
                android:text="1"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_4"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_2"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:layout_toLeftOf="@+id/cmd_2"
                android:background="@drawable/zahlen"
                android:text="4"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_8"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_alignBaseline="@+id/cmd_7"
                android:layout_alignBottom="@+id/cmd_7"
                android:layout_toLeftOf="@+id/cmd_6"
                android:background="@drawable/zahlen"
                android:text="8"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_0"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:background="@drawable/zahlen"
                android:text="0"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_7"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_4"
                android:layout_toLeftOf="@+id/cmd_2"
                android:background="@drawable/zahlen"
                android:text="7"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_5"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_2"
                android:layout_alignLeft="@+id/cmd_2"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:background="@drawable/zahlen"
                android:text="5"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_point"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_alignLeft="@+id/cmd_2"
                android:layout_alignParentBottom="true"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:background="@drawable/zahlen"
                android:text="."
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_back_betrag"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_alignParentBottom="true"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:layout_toRightOf="@+id/cmd_point"
                android:background="@drawable/zahlen"
                android:text="&lt;-"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_clear"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_alignParentBottom="true"
                android:layout_marginTop="2dp"
                android:layout_toRightOf="@+id/cmd_back_betrag"
                android:background="@drawable/zahlen"
                android:text="C"
                android:textSize="25dp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/cmd_percent"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_back_betrag"
                android:layout_marginTop="2dp"
                android:layout_toRightOf="@+id/cmd_back_betrag"
                android:background="@drawable/zahlen"
                android:text="%"
                android:textSize="25dp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/cmd_3"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_back_betrag"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:layout_toLeftOf="@+id/cmd_clear"
                android:background="@drawable/zahlen"
                android:text="3"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_6"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_percent"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:layout_toLeftOf="@+id/cmd_percent"
                android:background="@drawable/zahlen"
                android:text="6"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_9"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_5"
                android:layout_toRightOf="@+id/cmd_8"
                android:background="@drawable/zahlen"
                android:text="9"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_changesite"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_percent"
                android:layout_alignLeft="@+id/cmd_percent"
                android:layout_marginTop="2dp"
                android:background="@drawable/zahlen"
                android:text="L"
                android:textSize="25dp" />

            <Button
                android:id="@+id/cmd_2"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_above="@+id/cmd_clear"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:layout_toRightOf="@+id/cmd_0"
                android:background="@drawable/zahlen"
                android:text="2"
                android:textSize="25dp" />

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

Amu*_*are 6

问题在于父布局.它被设置为wrap_content.

父的宽度改变RelativeLayout,以match_parent最有可能的问题应该是固定的.

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/RelativeLayout01" >
Run Code Online (Sandbox Code Playgroud)

更新的答案

随着上面的问题是,当之间的偏好的变化01 ,回到0你永远不删除以前分配的rule.(你只打电话params.addRule(..) ).这是问题,因为现在它有ALIGN_PARENT_RIGHTALIGN_PARENT_LEFT.

要解决此问题,您可以使用以下removeRule()方法:

if(sharedpreference.getInt("CHANGESITE", 0) == 0){
    RelativeLayout relativeLayout= (RelativeLayout)findViewById(R.id.rl_changesite);
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();

    params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    relativeLayout.setLayoutParams(params);
    sharedpreference.setInt("CHANGESITE", 1);
}
else {
    RelativeLayout relativeLayout= (RelativeLayout)findViewById(R.id.rl_changesite);
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();

    params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    relativeLayout.setLayoutParams(params);
    sharedpreference.setInt("CHANGESITE", 0);
}
Run Code Online (Sandbox Code Playgroud)

仅支持此方法API LEVEL 17 (JELLY_BEAN_MR1).

对于较低版本,只需创建一个新的布局参数并将其分配给RelativeLayout如下:

if(sharedpreference.getInt("CHANGESITE", 0) == 0){
    RelativeLayout relativeLayout= (RelativeLayout)findViewById(R.id.rl_changesite);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT); // add other rules from XML also
    relativeLayout.setLayoutParams(params);
    sharedpreference.setInt("CHANGESITE", 1);
}
else {
    RelativeLayout relativeLayout= (RelativeLayout)findViewById(R.id.rl_changesite);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); // add other rules from XML also
    relativeLayout.setLayoutParams(params);
    sharedpreference.setInt("CHANGESITE", 0);
}
Run Code Online (Sandbox Code Playgroud)

注意:对于方法2,您必须以编程方式添加XML文件中的其他规则,因为每次都会创建一个新的布局参数实例,因此不再应用XML中定义的那些规则.