gpt4 book ai didi

android - ActionBarSherlock 带有图标和标题的列表导航

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:00:31 24 4
gpt4 key购买 nike

我正在开发一个应用程序,其中导航是通过使用操作栏上的微调器完成的。微调器项目也需要包含图标,因此我为此创建了自定义布局。问题是微调器相对于其项目来说太宽了。我正在使用 ActionBarSherlock。到目前为止我写的代码:

navigation_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:orientation="horizontal">

<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:padding="4dip"/>

<TextView
android:id="@+id/title"
style="?attr/spinnerItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:layout_gravity="center_vertical"/>

</LinearLayout>

navigation_list_dropdown_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >

<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:adjustViewBounds="true"
android:padding="4dip"/>

<TextView
android:id="@+id/title"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"
android:singleLine="true" />

</LinearLayout>

NavigationListAdapter.java:

public class NavigationListAdapter extends BaseAdapter{
private Drawable[] mIcons;
private String[] mTitles;
private Context mContext;
private LayoutInflater mInflator;

public NavigationListAdapter(Context context, Drawable[] icons, String[] titles){
mContext = context;
mInflator = (LayoutInflater)mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mIcons = icons;
mTitles = titles;
}

@Override
public int getCount() {
return mTitles.length;
}

@Override
public Object getItem(int position) {
return mTitles[position];
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = mInflator.inflate(R.layout.navigation_list_item, parent, false);
holder = new ViewHolder();
holder.icon = (ImageView)convertView.findViewById(R.id.icon);
holder.title = (TextView)convertView.findViewById(R.id.title);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}

holder.title.setText(mTitles[position]);
holder.icon.setImageDrawable(mIcons[position]);

return convertView;
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = mInflator.inflate(R.layout.navigation_list_dropdown_item, parent, false);
holder = new ViewHolder();
holder.icon = (ImageView)convertView.findViewById(R.id.icon);
holder.title = (TextView)convertView.findViewById(R.id.title);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}

holder.title.setText(mTitles[position]);
holder.icon.setImageDrawable(mIcons[position]);

return convertView;
}

private class ViewHolder{
public ImageView icon;
public TextView title;
}
}

Activity 的onCreate方法:

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Context context = getSupportActionBar().getThemedContext();
TypedArray ta = getResources().obtainTypedArray(R.array.leagues);
Drawable[] logos = new Drawable[ta.length()];
for(int i = 0; i < logos.length; i++){
logos[i] = ta.getDrawable(i);
}

NavigationListAdapter adapter = new NavigationListAdapter(context, logos,
getResources().getStringArray(R.array.league_names));

getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(adapter, null);
}

问题说明: enter image description here

最佳答案

也许将 navigation_list_dropdown_item.xml 中的 TextView 的宽度更改为 wrap_content 生效(如果更改 LinearLayout 的宽度不起作用)。你可以看到这个:http://dandar3.blogspot.com.es/2013/03/actionbarsherlock-custom-list-navigation.html .

关于android - ActionBarSherlock 带有图标和标题的列表导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14599385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com