在动作条宽两微调菜单项 [英] Two Spinner menu items in ActionBar width
问题描述
我手动建在操作栏两个微调器,通过创建在main.xml中的文件前两个菜单项。随着行
I've manually built two spinners in the Action bar, by creating at first two menu items in the main.xml file. With the line
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 left spinners resource string is so big that just a small bit of the right spinner can be seen.
我试着像 cSpinner.setLayoutParams(新Spinner.LayoutParams(60,20));
或
ViewGroup.LayoutParams params = pView.getLayoutParams();
params.width = 100;
cspinner.setLayoutParams(params);
但他们没有工作。第二个,甚至让程序崩溃。 你现在知道小费我怎么能解决这个问题?
But none of them work. The second one even lets the program crash. Do you now know a tip how I can solve this problem?
推荐答案
有关这一点,你必须添加自定义布局动作条中包含
for this you have to add custom Layout in ActionBar like Below
code是这里
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
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>
这篇关于在动作条宽两微调菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!