gpt4 book ai didi

android - 操作栏 Sherlock NAVIGATION_MODE_LIST - dropdownListPreferredItemHeight 被忽略

转载 作者:太空狗 更新时间:2023-10-29 16:20:41 24 4
gpt4 key购买 nike

我正在使用带有导航模式 NAVIGATION_MODE_LIST自定义微调器适配器 的操作栏 Sherlock。

问题是下拉 ListView 看起来被挤压了,项目的高度看起来像 wrap_content 而不是倾斜的固定高度。 ( this is how it looks )

根据*abs_themes*,高度应该是48dip:

<item name="dropdownListPreferredItemHeight">48dp</item>

如果我使用从资源创建的非自定义微调器适配器,它可以正常工作,我缺少什么?这是我编写的示例代码:

MainActivity.java

package com.example.themetest;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.app.SherlockActivity;

public class MainActivity extends SherlockActivity {

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

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

final String[] choices = {"Android", "iOS", "RIM"};
CustomSpinnerAdapter customSpinnerAdapter = new CustomSpinnerAdapter(MainActivity.this, android.R.id.text1, choices);
actionBar.setListNavigationCallbacks(customSpinnerAdapter, new OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Toast.makeText(MainActivity.this, choices[itemPosition], Toast.LENGTH_SHORT).show();
return false;
}
});

// this works properly
// actionBar.setListNavigationCallbacks(ArrayAdapter.createFromResource(this, R.array.sections, R.layout.sherlock_spinner_dropdown_item),
// new OnNavigationListener() {
// public boolean onNavigationItemSelected(int itemPosition, long itemId) {
// return false;
// }
// });
}

private class CustomSpinnerAdapter extends ArrayAdapter<String> implements SpinnerAdapter {
private LayoutInflater layoutInflater;

public CustomSpinnerAdapter(Context context, int textViewResourceId, String[] values) {
super(context, textViewResourceId, values);
this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
return initView(position, convertView);
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return initView(position, convertView);
}

private View initView(int position, View convertView) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.sherlock_spinner_dropdown_item, null);
}
((TextView) convertView.findViewById(android.R.id.text1)).setText(getItem(position));
return convertView;
}
}

}

styles.xml

<resources>
<style name="Theme.AppTheme" parent="Theme.Sherlock.Light" />
</resources>

list .xml

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppTheme" >
<activity
android:name="com.example.themetest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

最佳答案

您没有将 parent 传递给 inflate() 方法。这应该是忽略高度的原因。

layoutInflater.inflate(R.layout.sherlock_spinner_dropdown_item, parent, false);

代替

layoutInflater.inflate(R.layout.sherlock_spinner_dropdown_item, null);

像这样实现 getView()getDropDownView():

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(
R.layout.sherlock_spinner_item, parent, false);
}
((TextView) convertView.findViewById(android.R.id.text1))
.setText(getItem(position));
return convertView;
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(
R.layout.sherlock_spinner_dropdown_item, parent, false);
}
((TextView) convertView.findViewById(android.R.id.text1))
.setText(getItem(position));
return convertView;
}

关于android - 操作栏 Sherlock NAVIGATION_MODE_LIST - dropdownListPreferredItemHeight 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15798705/

24 4 0