代码之家  ›  专栏  ›  技术社区  ›  mrmeaaan

为微调器项指定其下拉列表的宽度

  •  0
  • mrmeaaan  · 技术社区  · 7 年前

    我的布局中有一个微调器问题。

    看看这张照片有什么想法。

    enter image description here

    没有简单的方法将项目宽度设置为实际的下拉菜单宽度吗?

    <android.support.v7.widget.AppCompatSpinner
        android:id="@+id/spellSpinner"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:dropDownWidth="match_parent"
        android:spinnerMode="dropdown"
        android:popupBackground="#fff"
        android:background="#8A8A8A"/>
    

    我的Java代码:

    spinnerSpells = findViewById(R.id.spellSpinner);
    ArrayAdapter < CharSequence > adapter = ArrayAdapter.createFromResource(spellActivity.this, R.array.dropdownCategory, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerSpells.setAdapter(adapter);
    
    spinnerSpells.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView << ? > adapterView, View view, int i, long l) {
        getSpellList(spinnerSpells.getSelectedItem().toString(), switchSpells.isChecked());
        // do some other stuff [...]
      }
    
      @Override
      public void onNothingSelected(AdapterView << ? > adapterView) {
        getSpellList(spinnerSpells.getSelectedItem().toString(), switchSpells.isChecked());
       // do some other stuff [...]
      }
    });
    

    致以最诚挚的问候

    另外,我真的没有在论坛上找到任何关于那件事的帖子。

    5 回复  |  直到 7 年前
        1
  •  1
  •   mrmeaaan    7 年前

    老实说,他们的回答并没有真正帮助我,但给了我很好的暗示。我自己找到了解决问题的方法(还实现了标签前面的图像)。

    我张贴我的代码:

    package edmt.dev.androidgridlayout;
    
    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    public class xCustomSpinnerAdapater extends ArrayAdapter<String> {
    
        Context mContext;
        String[] spinnerNames;
        int[] spinnerImages;
    
        public xCustomSpinnerAdapater( Context mContext, String[] spinnerNames, int[] spinnerImages) {
            super(mContext, R.layout.x_custom_spinner, spinnerNames);
            this.mContext = mContext;
            this.spinnerNames = spinnerNames;
            this.spinnerImages = spinnerImages;
        }
    
    
        @Override
        public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    
                LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View row = inflater.inflate(R.layout.x_custom_spinner,null);
                TextView tvSpinnerText = row.findViewById(R.id.tvSpinnerText);
                ImageView ivSpinnerImage = row.findViewById(R.id.ivSpinnerImage);
    
                tvSpinnerText.setText(spinnerNames[position]);
                ivSpinnerImage.setImageResource(spinnerImages[position]);
    
            return row;
        }
    
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    
            LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.x_custom_spinner,null);
            TextView tvSpinnerText = row.findViewById(R.id.tvSpinnerText);
            ImageView ivSpinnerImage = row.findViewById(R.id.ivSpinnerImage);
    
            tvSpinnerText.setText(spinnerNames[position]);
            ivSpinnerImage.setImageResource(spinnerImages[position]);
    
            return row;
        }
    }

    相应地添加了一个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="40dp"
        android:orientation="horizontal"
        >
    
        <ImageView
            android:id="@+id/ivSpinnerImage"
    
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
    
            android:padding="5dp"
            android:src="@drawable/artifact"
            />
        <TextView
            android:id="@+id/tvSpinnerText"
    
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="7"
            android:gravity="center"
    
            android:text="Accessory"
            android:textSize="18sp"
    
            />
    
    </LinearLayout>

    之后我编辑了我的主要活动如下:

    [..]
    
      private xCustomSpinnerAdapater spinnerAdapter;
        private String[] spinnerNames = {"All", "Black", "Blue", "Green", "Red"};
        private  int[] spinnerImages = {R.drawable.artifact, R.drawable.black, R.drawable.blue, R.drawable.green, R.drawable.red};
    
    
    [..]
    
    protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_spell);
            
            spinnerSpells = findViewById(R.id.spellSpinner);
            spinnerAdapter = new xCustomSpinnerAdapater(this, spinnerNames,spinnerImages);
            spinnerSpells.setAdapter(spinnerAdapter);
    
    
            spinnerSpells.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    Toast.makeText(getApplicationContext(),spinnerNames[i],Toast.LENGTH_LONG).show();
                   [...]
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {
                    [...]
            });

        2
  •  0
  •   Vijaya Varma Lanke    7 年前

    为微调器项使用自定义XML文件。

    <?xml version="1.0" encoding="utf-8"?>
    
    <TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:gravity="left"  
    android:textColor="#FF0000"         
    android:padding="5dip"
    />
    

    使用此文件可显示微调器项,如:

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
    R.layout.spinner_item,list);
    
        3
  •  0
  •   karan    7 年前

    为微调器textview创建单独的布局

    R.layout.spinner\u文本

    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@android:id/text1"
        android:maxLines="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="your_size_in_dp" />
    

    设置适配器如下

    ArrayAdapter < CharSequence > adapter = ArrayAdapter.createFromResource(spellActivity.this, R.array.dropdownCategory, R.layout.spinner_text);
    
        4
  •  0
  •   Gowthaman M manas.abrol    7 年前

    使用 在xml中

        android:dropDownWidth="fill_parent| match_parent| wrap_content"
        android:spinnerMode="dropdown"
    

    有关详细信息,请单击: https://developer.android.com/reference/android/widget/Spinner.html#setDropDownWidth(int)

        5
  •  0
  •   Pramila Shankar    7 年前
    1. 微调器数组列表将其设为“dropdowncegory.xml”

      <?xml version="1.0" encoding="utf-8"?>
      <resources>
          <string-array name="dropdownCategory">
              <item>All</item>
              <item>Black</item>
              <item>Blue</item>
              <item>Green</item>
              <item>Red</item>
          </string-array>
      </resources>
      
    2. <android.support.v7.widget.AppCompatSpinner
       android:id="@+id/spellSpinner"
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:layout_weight="1"
       android:layout_marginStart="10dp"
       android:layout_marginEnd="10dp"
       android:dropDownWidth="wrap_content"
       android:spinnerMode="dropdown"
       android:popupBackground="#fff"
       android:background="#8A8A8A"/>
      
    3. 在onCreate的activity.java类中

      Spinner spinnerSpells = findViewById(R.id.spellSpinner);
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(spellActivity.this, R.array.dropdownCategory, android.R.layout.simple_spinner_item);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinnerSpells.setAdapter(adapter);
      
      spinnerSpells.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
       // do something [...]
       }
      
      @Override
      public void onNothingSelected(AdapterView<?> adapterView) {
         // do something [...]
      });