Best way to inflate browsing programmatically

I found a simple SwipeSample, which I modified to allow me to create new xml layouts and inflate the main layout to display them. What I wanted to do was also able to programmatically add layouts for the swipe process.

I have a main.xml and red.xml and yellow.xml layout, which are a simple linearlayout with text set to solid color.

The code below works, but I don’t think this is the right or best way to do what I am trying to get. If anyone can suggest a better way, that would be greatly appreciated.

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //Create a layout with a solid blue background programmatically TextView tv1 = new TextView(this); tv1.setText("Blue"); tv1.setBackgroundColor(Color.BLUE); tv1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); ll.addView(tv1); //Create a layout with a solid green background programmatically TextView tv2 = new TextView(this); tv2.setText("Green"); tv2.setBackgroundColor(Color.GREEN); tv2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); LinearLayout ll2 = new LinearLayout(this); ll2.setOrientation(LinearLayout.VERTICAL); ll2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); ll2.addView(tv2); //inflate the flipper view and add the yellow and red xml layouts and also the 2 programmatically created layouts fSpace = (ViewFlipper)findViewById(R.id.flipper); inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.yellow, fSpace); inflater.inflate(R.layout.red, fSpace); fSpace.addView(ll); fSpace.addView(ll2); } 
+6
source share
2 answers

If you have a complex layout that you want to create programmatically, it might be easiest to make the layout in xml and then just inflate it and add it at run time.

Create view in xml

Here is a sample of the finished xml layout, which is located in the layout folder. Yours can be any, single view or complex layout.

Layout / my_view.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/TextView1" android:text="This is a TV"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/TextView2" android:text="How are you today?"/> </LinearLayout> 

Create a container to view

Put space in your layout for your work. You may have something like this.

 <FrameLayout android:id="@+id/flContainer" android:layout_width="wrap_content" android:layout_height="wrap_content"> </FrameLayout> 

Click view

Use the link to the container, inflate your view from xml and add it to the container.

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FrameLayout container = (FrameLayout) findViewById(R.id.flContainer); View inflatedLayout= getLayoutInflater().inflate(R.layout.my_view, null, false); container.addView(inflatedLayout); } 

Doing this in this way makes your code much cleaner.

See also:

+4
source

The way you inflate R.layout.yellow and R.layout.red is really suitable for this. Perhaps you can simplify your code by moving it to xml. I believe tv1 is just a sample? If not, it can go to main.xml. You can even find a way to create yellow and red colors with one inflation ... depending on what you do.

Programmatically creating views is simply for the most part a little tedious.

+2
source

Source: https://habr.com/ru/post/900256/


All Articles