【问题标题】:how to Call Android Layout(XML) Programmatically?如何以编程方式调用 Android 布局(XML)?
【发布时间】:2015-08-25 03:19:33
【问题描述】:

我想在函数中以编程方式调用 Android Layout(XML)。 布局已在 XML 中创建。我问这个问题是因为我不想在 Android Native 应用程序中使用它,实际上我会在 Unity3D 中调用这些布局,它是一个游戏引擎。所以假设我有一个布局。例如看下面的代码:

    <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>

现在我想创建一个可以调用此布局的函数。但是我不想使用下面的代码,因为我不会在 Android Native App 中使用。

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

我将直接使用Unity3D类如下:

public class MainActivity extends UnityPlayerActivity {

}

所以我需要在那个类中调用我的布局,但是以某些函数的形式。例如:

public class MainActivity extends UnityPlayerActivity {

        public void ShowLayout(){
             enter code here
        }
}

所以我需要你们帮助解决这个问题。

任何帮助将不胜感激。

【问题讨论】:

  • 您在寻找 LayoutInflater (developer.android.com/intl/ja/reference/android/view/…) 吗?
  • @innoSPG 我用 LayoutInflater 进行检查,但效果不佳。
  • @innoSPG 请查看此链接,因为这是我正在寻找的。 (thegamecontriver.com/2015/04/…)
  • @Mediasoft 你想以编程方式创建这个布局吗..
  • @Vishwa 不,我不想以编程方式创建布局。我使用 XML 创建的布局,不知何故我需要在函数中调用它。正如您在上面阅读的描述,我不会在 Android Native App 中使用。我将在使用 Unity3d 引擎的游戏中使用。所以从那个游戏中我需要调用 Native Layouts。

标签: java android xml android-layout unity3d


【解决方案1】:

使用inflator 进行布局,然后将其放在前面。

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

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

【讨论】:

    【解决方案2】:

    改为定义您的视图/布局而不使用 XML。在您的情况下,您可以在 ShowLayout() 方法中定义它。此代码应为您指明正确的方向:

        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));
    

    【讨论】:

    • @Joakim 是的,我知道这种方式,但问题是如果我想创建复杂布局并且使用编程方式绘制将非常困难。所以使用 XML 创建是很容易。
    • 啊,当然。你看过这个帖子吗? stackoverflow.com/questions/21236094/… 。特别是关于View rootView = findViewById(android.R.id.content); 的部分,其中content 将是您在根线性布局上设置的id?
    • @Joakim 实际上这是相反的情况。他们关心将 Unity3d 调用到 Android 中,但在我的情况下,我需要将 Android 调用到 Unity3d 中。哪个不同?
    • 我认为我对此了解的不够多,无法帮助您。在我看来是一样的。
    猜你喜欢
    • 1970-01-01
    • 2011-03-12
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    相关资源
    最近更新 更多