2013-04-01 221 views
1

說我的佈局有一個按鈕與左邊對齊,並且TextView與右邊對齊。Android:水平翻轉佈局

我希望對於某些語言,佈局將按原樣顯示,但對於其他語言,按鈕應位於右側,而文本應位於左側。

是否有一些內置或簡單的方法來動態翻轉佈局視圖?

我可以創建一個differenet佈局並動態設置內容視圖,但如果有更好的建議,我寧願避免使用此解決方案。

感謝

回答

1

在這裏,在我的示例代碼,我改變的按鈕佈局點擊你可以把你需要同樣

if laguage = "bl bla" call layout1(); 
else call layout2(); 

我不是創建不同的佈局,根據登錄進來,而是相同的佈局我正在動態分配不同的佈局屬性。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

     <Button 
     android:id="@+id/btn1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@android:color/black" 
     android:background="@drawable/cuadrogris" 
     android:text="Button 1" 
     android:textSize="20sp" /> 

    <Button 
     android:id="@+id/btn2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@android:color/black" 
     android:background="@drawable/cuadrogris" 
     android:layout_below="@id/btn1" 
     android:text="Button 2" 
     android:textSize="20sp" /> 


    <Button 
     android:id="@+id/btnctrl1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@android:color/black" 
     android:background="@drawable/cuadrogris" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:text="Alignment 1" 
     android:textSize="20sp" /> 

    <Button 
     android:id="@+id/btnctrl2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@android:color/black" 
     android:background="@drawable/cuadrogris" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true"  
     android:text="Alignment 2" 
     android:textSize="20sp" /> 

</RelativeLayout> 






public class TestActivity extends Activity { 

    Button mbtn1; 
    Button mbtn2; 

    Button mbtncontroller1; 
    Button mbtncontroller2; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.relative_layout_activity); 

     initializeControls(); 

    } 

    private void initializeControls() { 
     // TODO Auto-generated method stub 

     mbtn1 = (Button)findViewById(R.id.btn1); 
     mbtn2 = (Button)findViewById(R.id.btn2); 
     mbtncontroller1 = (Button)findViewById(R.id.btnctrl1); 
     mbtncontroller2 = (Button)findViewById(R.id.btnctrl2); 


     //I am changing the layout here on Button clicks named(mbtncontroller1 and mbtncontroller2 respectively) 
     //You can do the same according to you condition if the language = "bla bla" this layout else that layout 


     mbtncontroller1.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       layout1(); 

      } 
     }); 


     mbtncontroller2.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 


       layout2(); 
      } 
     }); 


    } 

    private void layout1() 
    { 
     //These layout params actually help in accessing the properties of relative layout or Linear layout (here we are using Relative Layout) 
       RelativeLayout.LayoutParams relativeParamsleft = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
       RelativeLayout.LayoutParams relativeParamsright = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 

       relativeParamsleft.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);// helps in assging properties of relative layout that actually we easily get in XML layout 
       mbtn2.setLayoutParams(relativeParamsleft); // we need to set these layout params to the View. 

       relativeParamsright.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
       mbtn1.setLayoutParams(relativeParamsright); 

    } 

    private void layout2() 
    { 

     RelativeLayout.LayoutParams relativeParamsleft = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
     RelativeLayout.LayoutParams relativeParamsright = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 

     relativeParamsleft.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
     mbtn2.setLayoutParams(relativeParamsleft); 

     relativeParamsright.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
     mbtn1.setLayoutParams(relativeParamsright); 

    } 

    }