我尝试创建一个应用程序,用户可以为大型设备设置“单手布局”,只用一只手输入数字. (看看我的意思)
当我按下“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); } }});
布局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="<-" 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>
解决方法:
问题在于父布局.它设置为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" >
更新的答案
除此之外,问题在于,当首选项在0到1之间变化并返回0时,您永远不会删除先前分配的规则. (你只能调用params.addRule(..)).这是问题,因为它现在同时具有AliGN_PARENT_RIGHT和AliGN_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);}
仅从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);}
注意:对于方法2,您必须以编程方式添加XML文件中的其他规则,因为每次都会创建一个新的布局参数实例,因此不再应用XML中定义的那些规则.
总结以上是内存溢出为你收集整理的Android:设置ALIGN_PARENT_RIGHT不起作用全部内容,希望文章能够帮你解决Android:设置ALIGN_PARENT_RIGHT不起作用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)