Two Spinner menu items in ActionBar width

I manually built two counters on the action bar, first creating two menu items in the main.xml file. With string

cSpinner.setAdapter( ArrayAdapter.createFromResource( this,
            R.array.category_data,
            android.R.layout.simple_spinner_dropdown_item )
            );

I set the Array resource for them. These things actually work, but the problem is that the resource line of the left rotator is so large that you can see only a small piece of the right counter.

  I tried something like cSpinner.setLayoutParams(new Spinner.LayoutParams(60, 20));or '

ViewGroup.LayoutParams params = pView.getLayoutParams();
         params.width = 100;
            cspinner.setLayoutParams(params);

But none of them work. The second even allows the program to crash. Now you know the advice, how can I solve this problem?

+5
source share
1 answer

for this you need to add a custom layout to the ActionBar as shown below

enter image description here

enter image description here


code here

public class MainActivity extends Activity {

    final String[] choices = { "Android", "iOS", "RIM" };

    private Spinner Spin1;
    private Spinner Spin2;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                MainActivity.this, android.R.layout.simple_dropdown_item_1line,
                choices);
        final ActionBar actionBar = getActionBar();
        actionBar.setCustomView(R.layout.actionbar_item);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);

        Spin1 = (Spinner) findViewById(R.id.spinner1);
        Spin2 = (Spinner) findViewById(R.id.spinner2);

        Spin1.setAdapter(adapter);
        Spin2.setAdapter(adapter);
    }

}

actionbar_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="5" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="my App name"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#000000" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

</LinearLayout>
+10

All Articles