【问题标题】:Give spinner items the width of its dropdown为微调器项目提供其下拉列表的宽度
【发布时间】:2018-10-08 10:27:43
【问题描述】:

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

问题是我的项目宽度很小,在小屏幕上很难点击它们。

检查图片的想法。

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

<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 [...]
  }
});

致以最诚挚的问候 CG

附:我真的没有在论坛上找到任何关于这件事的帖子。

【问题讨论】:

  • 这取决于您在微调器适配器中设置的布局。您可以在将适配器设置为微调器的地方发布 java 代码吗?
  • 确定我会编辑我的帖子。
  • 让它包装内容:android:dropDownWidth="match_parent"
  • @DiwakarSingh 这与下拉菜单的宽度无关。它与您可以在下拉菜单中单击的标签的宽度有关。

标签: android xml android-studio


【解决方案1】:

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

基本上,使用专用 xml 文件为微调器充气是正确的调用。 我发布我的代码:

为我的 adater 创建了一个新类:

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>

然后我编辑了我的 mainActivity 如下:

[..]

  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) {
                [...]
        });

Linearlayout中xml文件中layout_width = "match_parent"的使用解决了我的问题。

致以最诚挚的问候 CG

【讨论】:

    【解决方案2】:

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

    说 spinner_item.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】:

    为微调器文本视图创建单独的布局

    R.layout.spinner_text

    <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】:

    在xml中使用属性

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

    更多点击: https://developer.android.com/reference/android/widget/Spinner.html#setDropDownWidth(int)

    【讨论】:

    • 这不是为了让 dropdownWidth 更长。它关于项目可以在下拉列表的任何位置单击,而不仅仅是文本。抱歉,这对我没有帮助。
    • @Christian.gruener 能否请您尝试将“ArrayAdapter 适配器”更改为“ArrayAdapter 适配器”我猜您创建的数组文件可能是字符串数组可能类似于this "本科生研究生" 你能检查一下吗……等一下我会分享我的答案
    • 并且使用 ArrayAdapter 您不需要在选择数组项后将其转换为字符串,因为您正在使用 '' spinnerSpells.getSelectedItem().toString() "
    • 将它链接到 确实给了我一个错误,因为它希望得到一个 CharSequence。老实说,我对此感到非常困惑,并没有弄清楚问题是什么或为什么数组字符串必须是 CharSequence。
    • 是否可以通过邮件联系到您。所以我可以分享一些我在大多数应用程序中使用过的类似代码。您可能在日志中有任何错误,如果可用,请也分享这些错误。
    【解决方案5】:
    1. 微调器数组列表让它成为'dropdownCategory.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 [...]
      });
      

    希望对你有帮助

    【讨论】:

    • 遗憾的是这并没有帮助。 " new ArrayAdapter(spellActivity.this, R.array.dropdownCategory, android.R.layout.simple_spinner_item);"胖子只会给我错误,因为 Java “期望资源类型布局..”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 2017-02-21
    相关资源
    最近更新 更多