如何使用一个按钮更改布局顺序?

时间:2013-10-10 21:03:09

标签: android android-layout relativelayout layoutparams

我创建了一个包含3个RelativeLayout的布局。其中2个是不可见的,我想让它们可见,当我按下一个按钮时,它会显示在顶部。可能吗?

如何制作android:

layout_below="@id/pag6a_1"

编程?


这让我错了!!这是我的代码:`protected void onCreate(Bundle savedInstanceState){         super.onCreate(savedInstanceState);         的setContentView(R.layout.pag6a);

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

    pag6a_2.setVisibility(View.VISIBLE);
    params.addRule(RelativeLayout.BELOW, R.id.pag6a_3);`

..这是我的logcat:

  

10-10 21:27:11.025:E / AndroidRuntime(3117):致命异常:主要   10-10 21:27:11.025:E / AndroidRuntime(3117):java.lang.RuntimeException:无法启动活动ComponentInfo {com.beandroid.pirani / com.beandroid.pirani.Pag6A}:java.lang.ClassCastException:android .widget.LinearLayout $ LayoutParams无法强制转换为android.widget.RelativeLayout $ LayoutParams   10-10 21:27:11.025:E / AndroidRuntime(3117):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)   10-10 21:27:11.025:E / AndroidRuntime(3117):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)   10-10 21:27:11.025:E / AndroidRuntime(3117):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)   10-10 21:27:11.025:E / AndroidRuntime(3117):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)   10-10 21:27:11.025:E / AndroidRuntime(3117):在android.os.Handler.dispatchMessage(Handler.java:99)   10-10 21:27:11.025:E / AndroidRuntime(3117):在android.os.Looper.loop(Looper.java:137)   10-10 21:27:11.025:E / AndroidRuntime(3117):在android.app.ActivityThread.main(ActivityThread.java:5041)   10-10 21:27:11.025:E / AndroidRuntime(3117):at java.lang.reflect.Method.invokeNative(Native Method)   10-10 21:27:11.025:E / AndroidRuntime(3117):at java.lang.reflect.Method.invoke(Method.java:511)   10-10 21:27:11.025:E / AndroidRuntime(3117):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)   10-10 21:27:11.025:E / AndroidRuntime(3117):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)   10-10 21:27:11.025:E / AndroidRuntime(3117):at dalvik.system.NativeStart.main(Native Method)   10-10 21:27:11.025:E / AndroidRuntime(3117):引起:java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.widget.RelativeLayout $ LayoutParams   10-10 21:27:11.025:E / AndroidRuntime(3117):at com.beandroid.pirani.Pag6A.onCreate(Pag6A.java:44)   10-10 21:27:11.025:E / AndroidRuntime(3117):在android.app.Activity.performCreate(Activity.java:5104)   10-10 21:27:11.025:E / AndroidRuntime(3117):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)   10-10 21:27:11.025:E / AndroidRuntime(3117):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)   10-10 21:27:11.025:E / AndroidRuntime(3117):... 11更多

2 个答案:

答案 0 :(得分:1)

我想你正在寻找这样的东西.-

RelativeLayout yourLayout = (RelativeLayout) findViewById(R.id.yourLayoutId);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) yourLayout.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.pag6a_1);

答案 1 :(得分:0)

如果我的问题正确,最好有一个Linearlayout。其中ViewFlipperbutton设置RelativeLayout内的ViewFlipper

ViewFlipper fliper = (ViewFlipper) findViewById(R.id.viewFlipperid);

并在按钮的onClick中添加`flipper.showNext'。