假设我的布局有一个左对齐的 Button 和一个右对齐的 TextView。
我希望对于某些语言,布局将按原样显示,但对于其他语言,按钮应位于右侧,而文本应位于左侧。
是否有一些内置或简单的方法来动态翻转布局视图?
我可以创建一个不同的布局并动态设置内容视图,但如果可以提出更好的建议,我宁愿避免这种解决方案。
谢谢
假设我的布局有一个左对齐的 Button 和一个右对齐的 TextView。
我希望对于某些语言,布局将按原样显示,但对于其他语言,按钮应位于右侧,而文本应位于左侧。
是否有一些内置或简单的方法来动态翻转布局视图?
我可以创建一个不同的布局并动态设置内容视图,但如果可以提出更好的建议,我宁愿避免这种解决方案。
谢谢
在我的示例代码中,我正在更改按钮单击上的布局您也可以根据您的需要进行登录
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);
}
}