相关疑难解决方法(0)

重用布局XML和背后的代码

我试图将几个按钮变成Android中的可重用组件.我已经成功地使XML/UI部分工作,但我无法弄清楚如何使代码背后的代码可以在活动之间重用,而不是在任何地方重新编码.我是Android的新手,所以如果这很明显我会道歉.

我已经多次审阅过这篇文章:Android布局技巧3 - 第1部分,但似乎缺少一些文件,而且我没有足够的经验来重建它们.

我的主要布局的一个愚蠢的版本:

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

    <WebView android:id="@+id/webview" 
        android:layout_width="fill_parent"
        android:layout_height="375px" 
        android:layout_alignParentTop="true" />

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

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

然后我的"组件":

<?xml version="1.0" encoding="UTF-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
      <LinearLayout   
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="center">

            <ImageButton android:id="@+id/Button1" 
                android:layout_width="71px"
                android:layout_height="wrap_content"
                android:background="@drawable/button1"              
                android:layout_alignParentBottom="true" />


            <ImageButton android:id="@+id/Button2" 
                android:layout_width="75px"
                android:layout_height="wrap_content"
                android:background="@drawable/button1"  
                android:layout_toRightOf = "@+id/Button1"
                android:layout_alignParentBottom="true" />              

        </LinearLayout>
        </merge>
Run Code Online (Sandbox Code Playgroud)

如果您对我的XML有任何其他批评,我也会很感激.

android

15
推荐指数
1
解决办法
8225
查看次数

标签 统计

android ×1