When creating an ImageView
for example, I know I can create it within a Layout
:
Example a LinearLayout
:
<LinearLayout...>
<ImageView
android:id="@id/hello_world_id"/>
</LinearLayout>
But can I define an View
outside a layout, and then add it to any other layout?
I want to have a RelativeLayout
which adds/removes views programmatically and dynamically, so that the RelativeLayout
starts off with no views inside it, then I add some, remove some etc. Would there be any way to do this? Or is it better just to have these views already inside some other Layout
, and then I add the Layout
(whatever it is--containing my view(s)) to my RelativeLayout
?
What you are searching for is LayoutInflator
Create a xml file - buttons.xml
<LinearLayout...> <ImageView android:id="@id/hello_world_id"/>
In your activity access it by
final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); View itemView = inflater.inflate(R.layout.buttons, null);// this is a layout in your master activity lLayout = (LinearLayout)findViewById(R.id.layout1); lLayout.addView(itemView);
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.