簡體   English   中英

如何在一個屏幕上制作兩個不同的小部件?

[英]how to make two different widgets in one screen?

我正在制作一個可以向用戶顯示不同手表的項目。手表具有不同的時鍾刻度盤。我想在一個屏幕上制作兩個不同的小部件(手表),並且手表具有不同的時間和時鍾刻度盤。 ?

您可以創建自己的控件,然后在單個屏幕中包含該控件兩次。

package com.sample.ui.control;

public class MyControl extends LinearLayout {
    public MyControlContext context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.control_my_control, this);
    }
}

您可以根據需要添加其他代碼。 xml文件control_my_control是您的手表的XML。 在該XML中,請確保外部元素是一個元素。

對於將包含其中兩個的視圖,您可以像這樣使用它們:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <com.sample.ui.control.MyControl android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/control1" />
    <com.sample.ui.control.MyControl android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/control2" />
</LinearLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM