![](/img/trans.png)
[英]How do you create a jar file of classes that can be re-used by multiple Android projects?
[英]How do you refer to separate instances of elements of a re-used layout in Android?
我創建了一個包含三個數字選擇器的片段布局。 我曾經在activity_main.xml中兩次使用此片段。 兩組數字選擇器均正確顯示,但是我不確定如何以編程方式操作它們,因為我不知道如何分別引用每個數字選擇器。 基本上,我想知道給定我當前的布局實現是否可以分別引用每個數字選擇器。
picker_fragment.xml代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/picker_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<NumberPicker
android:id="@+id/redPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<NumberPicker
android:id="@+id/greenPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<NumberPicker
android:id="@+id/bluePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
activity_main.xml代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.colorpickertwo.MainActivity"
tools:ignore="MergeRootFrame" >
<include layout="@layout/picker_fragment"/>
<include layout="@layout/picker_fragment"/>
</LinearLayout>
我可以放棄我的picker_fragment,或者進行第二個片段布局,以便提供第二組數字選擇器的獨立ID,但是我當前的實現似乎更簡潔。
非常感謝!
通過編程,您可以像下面這樣引用您的視圖:
NumberPicker redPicker = (NumberPicker)findViewById(R.id.redPicker);
NumberPicker greenPicker = (NumberPicker)findViewById(R.id.redPicker);
NumberPicker bluePicker = (NumberPicker)findViewById(R.id.bluePicker);
// and then do something with redPicker...
這是來自android的View文檔,請檢查ID部分。
我在這里找到答案! 這個人也比我更好地表達了我的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.