【问题标题】:How can i change included xml layout to another layout on java code?如何将包含的 xml 布局更改为 java 代码上的另一个布局?
【发布时间】:2012-01-25 06:26:36
【问题描述】:

我在第一个布局中加入了第二个布局,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="clip_horizontal"
    android:layout_alignParentLeft="true" 
    android:id="@+id/rlMenu"
    >

    <Button
        android:id="@+id/bMusteriler"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Musteriler"
        android:textSize="45dp"
        android:layout_alignParentLeft="true"
         />

</RelativeLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_toRightOf="@id/rlEkranlar"
     >

    <include
        android:id="@+id/include1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/ikinci" />

</RelativeLayout>
</RelativeLayout>

问题是如何在单击按钮时更改包含的布局(在 java 代码上)?

【问题讨论】:

  • 创建一个新的 xml 文件和活动,并通过意图调用第二个。
  • 请修改你的问题,不太清楚。
  • @YugandharBabu 我想在单击按钮时将 layout="@layout/ikinci" 更改为 layout="@layout/third_layout"。

标签: android android-layout xml-layout


【解决方案1】:

我建议在您的include 语句的RelativeLayout 中使用ViewFlipper。试试这样:

<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vf"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <include android:id="@+id/include1" layout="@layout/ikinci" />
    <include android:id="@+id/map" layout="@layout/third_layout" />

</ViewFlipper>

如下访问 ViewFlipper。 最初输出第一个布局:

ViewFlipper vf = (ViewFlipper)findViewById(R.id.vf);

对于按钮 onClickListener:

button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                vf.setDisplayedChild(1);
            }
        });

【讨论】:

  • 非常感谢。你解决了问题 =))
  • 为了体验,如果使用 ViewFlipper 可能你会看到你的应用有点慢......
【解决方案2】:

有两种方法可以改变代码中的布局:

A.改变可见性。在您的 xml 中包含两种不同的布局:

<include 
    android:id="@+id/id1"
    android:visibility="visible"/>
<include 
    android:id="@+id/id2"
    android:visibility="gone"/>

在代码中使用这个:

findViewById(R.id.id1).setVisibility(View.GONE);
findViewById(R.id.id2).setVisibility(View.VISIBLE);

B.手动添加孩子。使用与现在相同的 xml,并添加可以添加或删除子项的代码:

yourRelativeLayout.removeAllViews();
yourRelativeLayout.addView(viewToInclude);

题外话:

您不需要在RelativeLayout 中写xmlns:android 参数。仅用于布局文件中最顶部的标签。

【讨论】:

  • 我很困惑,因为 Intellij 14 在自动完成选项中不提供“可见性”。
【解决方案3】:

您是否尝试过获取包含其他 xml 并使用 findViewById() 的 xml 的当前视图的实例。视图 v 是第一个布局,针对此视图,您使用 .findViewById()

【讨论】:

  • 我试过这个:Layout L1 = (Layout) findViewById (R.id.layout1);有转换错误。无法将视图转换为布局。我做错了什么吗?
  • 布局? SDK中没有这样的View,只要你自己创建。确保您正确引用了类名。喜欢:LinearLayout、RelativeLayout、FrameLayout....
  • 我明白了,我会试试这个。谢谢
  • 我试过你的建议。但我无法解决我的问题。我想在单击按钮时更改包含的布局。我找不到解决这个问题的方法。我怎样才能做到这一点?谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
  • 1970-01-01
相关资源
最近更新 更多