【问题标题】:FrameLayout : bring one layout ontop of another layout programaticallyFrameLayout :以编程方式将一种布局置于另一种布局之上
【发布时间】:2016-06-05 19:45:51
【问题描述】:

我有一个由两个子线性布局组成的框架布局(一个在另一个之上) 我有两个按钮“1”和“2” 当我按 1 时,我希望第一个线性布局位于第二个线性布局之上

当我按 2 时,我希望第二个线性布局位于第一个线性布局之上

我使用了 bringToFront() 来执行此操作 但是什么也没发生

我的布局

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:gravity="center">

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/button1"
        android:text="1"/>

    <Button
        android:layout_height="wrap_content"
        android:text="2"
        android:layout_width="wrap_content"
        android:id="@+id/button2"/>

</LinearLayout>

<FrameLayout
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:layout_weight="1">

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:background="#D88681"
        android:id="@+id/firstLayout"/>

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:background="#57E8A7"
        android:id="@+id/secondLayout"/>

</FrameLayout>

我的主要活动

public class MainActivity extends Activity 
{

LinearLayout first, second;
Button btn1, btn2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    first = (LinearLayout) findViewById(R.id.firstLayout);
    second = (LinearLayout) findViewById(R.id.secondLayout);

    Button btn1 = (Button) findViewById(R.id.button1);

    btn1.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View p1)
            {
                first.bringToFront();
            }


    });


    btn2 = (Button) findViewById(R.id.button2);
    btn2.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View p1)
            {
                second.bringToFront();
            }
    });
   }
}

【问题讨论】:

    标签: android android-framelayout


    【解决方案1】:

    您可以使用 bringtofront() 方法来设置线性布局的可见性。 例如。

         first.setVisibility(View.GONE);
         second.setVisibility(View.VISIBLE);
    

    这将使第一个视图消失而第二个视图可见。

    【讨论】:

    • 但是如果父布局设置为不可见,子视图也会消失。我不希望这种情况发生
    • 好的。我真的不明白为什么你需要一个在另一个下面。为什么不把它们都放在同一个布局中
    • 我想要按钮按下时的叠加效果,所以将它们放在单一布局上不会给我这样的效果
    • 您想要动画效果。这在最初的问题中没有解释
    • 叠加效果我的意思是一个在另一个之上 ,,,sry 混淆
    【解决方案2】:

    忘记使父布局无效 添加这个

    myView.bringToFront();
        ((View)myView.getParent()).invalidate();
                    ((View)myView.getParent()).requestLayout();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多