在两个布局之间旋转位置
Rotate position between two layouts
我对如何在两个布局之间轻松旋转位置感兴趣 (RelativeLayout
)。我有两个 RelativeLayout
,我想在第一次点击时旋转它们的位置。
我正在尝试在控制台上创建动态位置,以便用户可以旋转他想要的位置。
我已经为此创建了自己的方法,但我想知道是否有更好更简单的解决方案。在那种方法中,我控制盒子的内容在哪里,并用动画旋转内容。
编辑:
这看起来像 main_activity:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/first"
android:layout_width="fill_parent"
android:layout_height="50dp"></RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/seconde"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_weight="1"></RelativeLayout>
<RelativeLayout
android:id="@+id/third"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_weight="1"></RelativeLayout>
</LinearLayout>
</LinearLayout>
您可以这样简化您的布局:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/first"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@android:color/holo_blue_bright" >
</RelativeLayout>
<View
android:id="@+id/viewSeparatorHorizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="50dp" />
<View
android:id="@+id/viewSeparatorVertical"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:id="@+id/seconde"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/viewSeparatorHorizontal"
android:layout_toLeftOf="@+id/viewSeparatorVertical"
android:background="@android:color/holo_green_dark" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/third"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/viewSeparatorHorizontal"
android:layout_toRightOf="@+id/seconde"
android:background="@android:color/holo_orange_dark" >
</RelativeLayout>
</RelativeLayout>
并将此代码用于 "rotate" 组件:
private RelativeLayout first;
private RelativeLayout third;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
first = (RelativeLayout) findViewById(R.id.first);
third = (RelativeLayout) findViewById(R.id.third);
}
public void invert(View v){
LayoutParams params1 = (RelativeLayout.LayoutParams)first.getLayoutParams();
LayoutParams params3 = (RelativeLayout.LayoutParams)third.getLayoutParams();
first.setLayoutParams(params3);
third.setLayoutParams(params1);
}
我对如何在两个布局之间轻松旋转位置感兴趣 (RelativeLayout
)。我有两个 RelativeLayout
,我想在第一次点击时旋转它们的位置。
我正在尝试在控制台上创建动态位置,以便用户可以旋转他想要的位置。
我已经为此创建了自己的方法,但我想知道是否有更好更简单的解决方案。在那种方法中,我控制盒子的内容在哪里,并用动画旋转内容。
编辑: 这看起来像 main_activity:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/first"
android:layout_width="fill_parent"
android:layout_height="50dp"></RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/seconde"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_weight="1"></RelativeLayout>
<RelativeLayout
android:id="@+id/third"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_weight="1"></RelativeLayout>
</LinearLayout>
</LinearLayout>
您可以这样简化您的布局:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/first"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@android:color/holo_blue_bright" >
</RelativeLayout>
<View
android:id="@+id/viewSeparatorHorizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="50dp" />
<View
android:id="@+id/viewSeparatorVertical"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:id="@+id/seconde"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/viewSeparatorHorizontal"
android:layout_toLeftOf="@+id/viewSeparatorVertical"
android:background="@android:color/holo_green_dark" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/third"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/viewSeparatorHorizontal"
android:layout_toRightOf="@+id/seconde"
android:background="@android:color/holo_orange_dark" >
</RelativeLayout>
</RelativeLayout>
并将此代码用于 "rotate" 组件:
private RelativeLayout first;
private RelativeLayout third;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
first = (RelativeLayout) findViewById(R.id.first);
third = (RelativeLayout) findViewById(R.id.third);
}
public void invert(View v){
LayoutParams params1 = (RelativeLayout.LayoutParams)first.getLayoutParams();
LayoutParams params3 = (RelativeLayout.LayoutParams)third.getLayoutParams();
first.setLayoutParams(params3);
third.setLayoutParams(params1);
}