简体   繁体   English

Android Linear布局更改在Unity上不可见

[英]Android Linear layout changes not visible onUnity

I had an android UI from sample which is shown below 我有一个来自示例的android UI,如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
     <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:weightSum="1" android:layout_height="wrap_content">
        <TextView android:text="data1:" 
            android:id="@+id/data1Label" 
            android:layout_weight="0.5"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
        </TextView>
        <TextView android:text="data2:" 
            android:id="@+id/data2Label" 
            android:layout_weight="0.5"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
        </TextView>     
    </LinearLayout>
    <ListView android:id="@+id/in"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        android:layout_weight="1"
    />
</LinearLayout>

Then I needed to add a combobox after the top label panel and, I added a Spinner component like below 然后,我需要在顶部标签面板后添加一个组合框,并且添加了一个Spinner组件,如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
     <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:weightSum="1" android:layout_height="wrap_content">
        <TextView android:text="data1:" 
            android:id="@+id/data1Label" 
            android:layout_weight="0.5"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
        </TextView>
        <TextView android:text="data2:" 
            android:id="@+id/data2Label" 
            android:layout_weight="0.5"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
        </TextView>     
    </LinearLayout>
    <Spinner
    android:id="@+id/cmd_spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
    <ListView android:id="@+id/in"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        android:layout_weight="1"
    />
</LinearLayout>

And In my Strings.xml resource file I added the data list for the spinner. 在我的Strings.xml资源文件中,添加了微调器的数据列表。

<string-array name="cmd_array">
        <item>cmd1</item>
        <item>cmd2</item>
        <item>cmd3</item>
    </string-array>

Then at the end of on create method of my activity class I added below method to set data to the spinner 然后在活动类的create方法的结尾,我添加了以下方法来将数据设置到微调器

Spinner spinner = (Spinner) findViewById(R.id.cmd_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
             R.array.cmd_array, android.R.layout.simple_spinner_item);
 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spinner.setAdapter(adapter);

But when I run my app it does not show me the spinner. 但是,当我运行我的应用程序时,它不会向我显示微调器。 Any idea about the issue? 对这个问题有什么想法吗?

UPDATE 更新

I am compiling my application as a android libaray project and the main activity is starting from Unity Game Engine. 我正在将应用程序编译为android libaray项目,并且主要活动是从Unity Game Engine开始的。

in onCreate() method set your spinner data in this way: 在onCreate()方法中,以这种方式设置微调器数据:

    Spinner spinner = (Spinner) findViewById(R.id.cmd_spinner);
    String[] array = getResources().getStringArray(R.array.cmd_array);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, array);
    spinner.setAdapter(adapter);

you should try this----- 你应该试试这个----

 Spinner spinner = (Spinner) findViewById(R.id.cmd_spinner);
 String[] stringArray = getResources().getStringArray(R.array.cmd_array);
 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, stringArray);
 spinner.setAdapter(arrayAdapter);

and you should modify your xml like below , since this is much better way.... 并且您应该像下面那样修改xml,因为这是更好的方法。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:text="data1:"
        android:id="@+id/data1Label"
        android:layout_weight="0.5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>

    <TextView
        android:text="data2:"
        android:id="@+id/data2Label"
        android:layout_weight="0.5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>
</LinearLayout>

<Spinner
    android:id="@+id/cmd_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<ListView
    android:id="@+id/in"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll"
    android:layout_weight="1" />

You can look at this answer . 您可以看一下这个答案 You can also go with a custom adapter, but the solution below is fine for simple cases. 您也可以使用自定义适配器,但是以下解决方案适用于简单情况。

Here's a re-post: 重新发布:

So if you came here because you want to have both labels and values in the Spinner - here's how I did it: 因此,如果您来这里是因为想要在Spinner中同时包含标签和值-这是我的操作方法:

Just create your Spinner the usual way Define 2 equal size arrays in your array.xml file -- one array for labels, one array for values Set your Spinner with android:entries="@array/labels" 只需按照通常的方式创建Spinner,即可在array.xml文件中定义2个相等大小的数组-一个用于标签的数组,一个用于值的数组使用android:entries =“ @ array / labels”设置Spinner

When you need a value, do something like this (no, you don't have to chain it): 当您需要一个值时,请执行以下操作(不,您不必链接它):

  String selectedVal = getResources().getStringArray(R.array.values)[spinner.getSelectedItemPosition()];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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