【问题标题】:Have ways for remain previous layout in all pages?有办法在所有页面中保留以前的布局吗?
【发布时间】:2012-10-31 12:24:23
【问题描述】:

当用户点击时,我想创建一个在页面底部有 5 个按钮的应用程序 在任何按钮上,页面将发生变化,但 5 个按钮仍保留在页面底部。现在我创建 这个程序,但是当我在其他类中调用 setContentView() 时,因为我调用了其他布局, 按钮被移除。有办法保留5个按钮吗?或者我应该在 5layout 中再次创建 5 个按钮?

谢谢 干杯

【问题讨论】:

  • 搜索基本活动,为所有类创建一个comman布局并将它们包含在您的类中...

标签: android layout button


【解决方案1】:

最简单的方法是在所有布局文件中制作一个包含 5 个按钮的布局 示例

<include layout="@layout/titlebar"/>

或者另一种方法是使用片段,这样只有改变的片段才能保持其他布局内容相同
编辑
使用&lt;merge&gt;标签
当在另一个布局中包含一个布局时,&lt;merge /&gt; 标签有助于消除视图层次结构中的冗余视图组。例如,如果您的主布局是一个垂直的 LinearLayout,其中两个连续的视图可以在多个布局中重复使用,那么放置两个视图的可重复使用的布局需要它自己的根视图。但是,使用另一个 LinearLayout 作为可重用布局的根会导致在垂直 LinearLayout 中出现垂直 LinearLayout。除了降低 UI 性能之外,嵌套的 LinearLayout 没有任何实际用途。

为了避免包含这样一个冗余的视图组,您可以将元素用作可重用布局的根视图。例如:

<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="@string/add"/>

    <Button
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="@string/delete"/>

</merge>

现在,当您将此布局包含在另一个布局中(使用标签)时,系统会忽略该元素并将两个按钮直接放置在布局中,代替标签。

请通过this链接获取更多关于重用布局的信息

【讨论】:

    【解决方案2】:

    据我所知,我认为您正在寻找标签而不是按钮。看看this

    【讨论】:

      【解决方案3】:

      感谢您的回答。我阅读了开发人员链接,并将合并和包含标记添加到 xml 文件,但程序有错误。为什么?

      MainActivity.xml

      <?xml version="1.0" encoding="utf-8"?>
      <include layout="@layout/location"/>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" >
      
      <WebView
          android:id="@+id/web_view"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1.0" />
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@color/background"
          android:gravity="bottom"
          android:orientation="horizontal" >
      
          <Button
              android:id="@+id/button_home"
              style="?android:attr/borderlessButtonStyle"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:drawableTop="@drawable/home_icon"
              android:text="@string/button_home"
              android:textColor="@color/text_home" />
      
          <Button
              android:id="@+id/button_product"
              style="?android:attr/borderlessButtonStyle"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:drawableTop="@drawable/product_icon"
              android:onClick="Product"
              android:text="@string/button_product"
              android:textColor="@color/text_product" />
      
          <Button
              android:id="@+id/button_places"
              style="?android:attr/borderlessButtonStyle"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:drawableTop="@drawable/places_icon"
              android:onClick="Places"
              android:text="@string/button_places"
              android:textColor="@color/text_places" />
      
          <Button
              android:id="@+id/button_rewards"
              style="?android:attr/borderlessButtonStyle"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:drawableTop="@drawable/rewards_icon"
              android:onClick="Rewards"
              android:text="@string/button_rewards"
              android:textColor="@color/text_rewards" />
      
          <Button
              android:id="@+id/button_more"
              style="?android:attr/borderlessButtonStyle"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:drawableTop="@drawable/more_icon"
              android:onClick="More"
              android:text="@string/button_more"
              android:textColor="@color/text_more" />
      </LinearLayout>
      
      </LinearLayout>
      

      location.xml

      <?xml version="1.0" encoding="utf-8"?>
      <merge xmlns:android="http://schemas.android.com/apk/res/android">
      
      <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">
      <TextView>
      <com.google.android.maps.MapView
          android:id="@+id/mapView"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:clickable="true"
          android:apiKey="0cPRv243zM1_S3ydsNg8MJP9_6BfCp642jOhPvQ"/>
      <LinearLayout
          android:id="@+id/zoom"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"/>
      
      </LinearLayout>
      </merge> 
      

      什么是不正确的? 谢谢

      【讨论】:

        猜你喜欢
        • 2014-05-22
        • 1970-01-01
        • 1970-01-01
        • 2021-12-12
        • 1970-01-01
        • 1970-01-01
        • 2019-02-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多