![](/img/trans.png)
[英]How can I access the layout in a class extending Activity that is to be subclassed by another Activity?
[英]Access Layout of another activity
我做了一個音樂播放器,這是設計快照
底部的設計對於所有標簽都是通用的。
這是Tabhost和通用設計的xml文件
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TabWidget>
</HorizontalScrollView>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.00"
android:divider="@color/list_divider" >
</FrameLayout>
<RelativeLayout
android:id="@+id/Player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/light_grey"
android:fadingEdgeLength="5dp"
android:weightSum="1" >
<TextView
android:id="@+id/song_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="1dp"
android:text="@string/all_you_need_is_love"
/>
<LinearLayout
android:id="@+id/player_art"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_below="@id/song_title"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/player_thumbnail"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/nocover"
android:padding="4dp"
android:contentDescription="@string/art_image" />
</LinearLayout>
<LinearLayout
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="@+id/player_art"
android:layout_alignTop="@+id/player_art"
android:paddingLeft="5dp"
>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_toRightOf="@+id/player_art"
android:layout_below="@+id/seekbar"
android:paddingBottom="3dp"
android:paddingLeft="5dp"
android:gravity="center_horizontal|center_vertical"
>
<ImageButton
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:padding="2dp"
android:scaleType="centerCrop"
android:src="@drawable/rsz_previous"
android:background="@drawable/custom_button"
android:contentDescription="@string/play_button"
/>
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:padding="2dp"
android:scaleType="centerCrop"
android:src="@drawable/rsz_play"
android:background="@drawable/custom_button"
android:contentDescription="@string/play_button"
/>
<ImageButton
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/rsz_next"
android:layout_marginLeft="10dp"
android:padding="2dp"
android:scaleType="centerCrop"
android:background="@drawable/custom_button"
android:contentDescription="@string/play_button"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</TabHost>
另一個xml文件用於顯示名為music_tab.xml的歌曲
這是我的Java文件,Allsong包含所有歌曲標題
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MainView = (ViewGroup) inflater.inflate(R.layout.music, null, false);
SongName = (TextView) MainView.findViewById(R.id.song_title);
SongName.setText(Allsong.get(position));
String prev = SongName.getText();
它不會在設計中設置歌曲標題,而是在設置之前具有setText()方法設置的值的上一個字符串。 請任何人幫助我。謝謝。
我也對此事做了很多研究,但沒有結果。 我得出的結論是,一個類只能訪問其展開的布局中存在的那些小部件。
據我所知,這是不可能的!
WIDGET-A
WIDGET-A
,您必須將對象從ClassA傳遞到ClassB (例如:使用意圖),然后對其進行操作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.