简体   繁体   中英

how to Call Android Layout(XML) Programmatically?

i want to Call Android Layout(XML) Programmatically in a function. Layout is already Created in XML. i am asking this question because i don't want to use this in Android Native app, actually i will call these layouts in Unity3D which is a Gaming Engine. So Let say that i have a layout. For Example look at below code:

  <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/useful_nums_item_name"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/useful_nums_item_value"/> </LinearLayout> <ImageButton android:layout_width="0dp" android:layout_height="wrap_content" android:src="@drawable/call" android:id="@+id/call_btn" android:onClick="callNumber"/> </LinearLayout> 

Now i want to create a function which can call this layout. But But i don't want to use below code as i am not going to use in Android Native App.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

i will directly use the Unity3D class as below:

public class MainActivity extends UnityPlayerActivity {

}

So i need to call my layout in that class but in form of Some Function. For Example:

public class MainActivity extends UnityPlayerActivity {

        public void ShowLayout(){
             enter code here
        }
}

So i need you people help to solve this problem.

Any help will be appreciated.

Take your layout with the use of inflator and then bring it on front.

LayoutInflater inflater = LayoutInflater.from(context);
View yourView = inflater.inflate(R.layout.popup_layout, null, false);

// then bring it to front
yourView.bringToFront();

Define your view / layout without XML instead. In your case, you could define it in the ShowLayout() method. This code should point you in the right direction:

    LinearLayout layout = new LinearLayout(getContext());
    layout.setWeightSum(1);
    LinearLayout childLayout = new LinearLayout(getContext());
    LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 0.8f);
    childLayout.setLayoutParams(childParams);

    LinearLayout.LayoutParams fieldParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    TextView name = new TextView(getContext());
    name.setLayoutParams(fieldParams);
    TextView value = new TextView(getContext());
    name.setLayoutParams(fieldParams);

    LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 0.2f);
    ImageButton call = new ImageButton(getContext());
    call.setLayoutParams(buttonParams);
    call.setImageResource(getResources().getDrawable(R.id.call));

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM