11

デフォルトでは、アクションバーリストでは、選択したアイテムのナビゲーション幅は最も幅の広いアイテムと同じです。google +、gmail、maps androidアプリにあるので、選択したアイテムに「wrap_content」を入れたいです。

誰かがそれをするのが辛いことを知っていますか?

ナビゲーションアダプタのgetViewを上書きしようとしましたが、機能しません。このビューは、最も幅の広いアイテムの幅を持つ別のビューでラップされているように見えます。スピナーでactionbar.setCustomビューも試しましたが、スピナーの動作は同じです。最も広いアイテム幅を使用します。

すべての提案、リンク、ヘルプをありがとう。

現在の状態

必要な状態

4

4 に答える 4

13

私は同じ問題に遭遇しました。Androidはすべてのアイテムに対してgetView()メソッドを呼び出し、最後に幅を最も広いアイテムの幅に設定するようです。

だからここに私の解決策があります:

  1. 現在選択されているセクション(例ではセクション)、たとえばselectedSectionをコードに保存します。実際には、NavigationListenerのonNavigationItemSelected()メソッドでそれを行う必要があります。

  2. SpinnerAdapterのgetView()メソッドをオーバーライドし、常にそのコンテンツをselectedSectionに設定します。

  3. NavigationListenerのonNavigationItemSelected()メソッドで、現在のモードをselectedSectionに設定した後、spinnerAdapterのnotifyDataSetChanged()メソッドを呼び出してみてください。

サンプルコードは次のとおりです。

final int selectedSection = 0;
final String sectionLabels[] = {"short sec", "long section", "looooonger section"};

final ArrayAdapter<String> arrayAdapter =
  new arrayAdapter<String>(this, simple_list_item_1) {
    @Override
    public int getCount() {
      // You need to implement here.
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.you_entry, null);
      }
      TextView textView = (TextView) convertView.findViewById(R.id.you_text);
      // Set the text to the current section.
      textView.setText(sectionLabels[selectedSection]);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
      // You need to implement here.
    }

  }

actionBar.setListNavigationCallbacks(navigationAdpater,
  new ActionBar.OnNavigationListener() {

    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
      // Store the current section.
      selectedSection = itemPosition;
      navigationAdpater.notifyDataSetChanged();
      return true;
    }
  });
于 2013-03-12T08:44:22.610 に答える
3

アダプタのgetView()メソッドをオーバーライドし、レイアウトパラメータをテキストビューに設定してコンテンツをラップする必要があります。スピナーは、選択したアイテムのサイズを自動的に変更します。

ArrayAdapterから拡張するカスタムアダプタを作成しました。

public class CustomAdapter extends ArrayAdapter<String> {

その後、getView()メソッドをオーバーライドして、現在のビューのLayoutParamsを変更します。

@Override 
public View getView(final int position, final View convertView, final ViewGroup parent) {
            View view = convertView;
            if (null == view) {
                view = LayoutInflater.from(this.getContext())
                         .inflate(android.R.layout.simple_list_item1, null);
                ((TextView) view).setText(getItem(position));
            }
            final LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                                               LayoutParams.WRAP_CONTENT);
            view.setLayoutParams(params);
            return view;
}

アクティビティで、アダプタインスタンスを作成し、スピナーを設定します。

List<String> stringList = new ArrayList<String>();
stringList.add("test1");
stringList.add("test12");
stringList.add("test123");
stringList.add("test1234");

final SpinnerAdapter adapter = new CustomAdapter(this.getContext(),
                                                 android.R.layout.simple_list_item1,
                                                 android.R.id.text1, 
                                                 stringList);
mySpinner.setAdapter(adapter);

リストから何かを選択すると、スピナーはビューを再作成し、選択したアイテムを最初の位置に並べ替えて、コンテンツのサイズに合わせてサイズを変更します。

于 2013-09-06T18:31:04.550 に答える
0

スピナーを作成し、それにテキストビューを追加します。スピナーで、maxWidthフィールドを任意の値に設定すると、これが処理されます。別のオプションは、

android:layout_width="0"
android_layout_weight="0.3"

これは、スピナーが画面幅の3分の1しか占有しないことを意味します。

テキストビューで設定します:

android:singleLine="false"
android:ellipsize="end"
于 2013-02-06T04:30:45.440 に答える
0

これは私のために働いた。重要な部分はこれです。以下のコードをアダプターとして配置し、selectedItemPositionを使用してオブジェクト配列からテキストを選択します。

int selectedItemPosition = position;
    if (parent instanceof AdapterView) {
        selectedItemPosition = ((AdapterView) parent)
                .getSelectedItemPosition();
    }

以下に例を示します。

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = getLayoutInflater();
    final View spinnerCell;
    if (convertView == null) {
        // if it's not recycled, inflate it from layout
        spinnerCell = inflater.inflate(R.layout.layout_spinner_cell, parent, false);
    } else {
        spinnerCell = convertView;
    }
    int selectedItemPosition = position;
    if (parent instanceof AdapterView) {
        selectedItemPosition = ((AdapterView) parent)
                .getSelectedItemPosition();
    }

    TextView title = (TextView) spinnerCell.findViewById(R.id.spinnerTitle);
    title.setText(titles[selectedItemPosition]);
    return spinnerCell;
}

説明が必要な場合は、次のリンクをたどってください:http: //coding-thoughts.blogspot.in/2013/11/help-my-spinner-is-too-wide.html

于 2016-02-07T08:11:55.970 に答える