如何通过多项选择调整 Spinner 中项目的高度大小?

发布于 2024-12-27 08:22:54 字数 191 浏览 2 评论 0原文

我只是根据这个 stackoverflow 答案(请参阅@Destil 回答)。这里我的问题是我无法通过多种选择来调整 Spinner 中项目的高度大小。如何调整每个项目的高度?

I just showing the spinner with multiple choice based on this stackoverflow answer(see @Destil answer). Here my problem is I can't re size the Height of the items in Spinner with multiple choice. How to re size the Height of each Items?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

度的依靠╰つ 2025-01-03 08:22:55

据我所知,您将必须使用自定义适配器,并重写 getDropDown 和 getView 方法。您将能够自定义每个项目的布局。

嗯...文字很好,例子更好,尝试这样的东西:

public class CustomStringArrayAdapter extends ArrayAdapter<String>
{
private Activity    myActivity;

public CustomStringArrayAdapter(Context context, int layoutResourceId, int textViewResourceId, List<String> objects, Activity act)
{
    super(context, layoutResourceId, textViewResourceId, objects);
    this.myActivity = act;
}

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

public View getCustomView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = myActivity.getLayoutInflater();
    View row = inflater.inflate(R.layout.spinner_layout, parent, false);
    TextView label = (TextView) row.findViewById(R.id.spinner_textview);
    label.setText(getItem(position));
    LinearLayout layout = (LinearLayout) row.findViewById(R.id.spinner_linear_layout);

    return row;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = myActivity.getLayoutInflater();
    View row = inflater.inflate(R.layout.spinner_layout, parent, false);
    TextView label = (TextView) row.findViewById(R.id.spinner_textview);
    label.setText(getItem(position));

    return row;
    }

}

和布局:(

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:gravity="center"
    android:background="@drawable/transparent"
    android:id="@+id/spinner_linear_layout">
    <TextView
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:gravity="center"
        android:layout_gravity="center"
        android:textSize="21sp"
        android:singleLine="true"
        android:id="@+id/spinner_textview"
        android:textColor="@color/black" />
</LinearLayout>

但这只是一个示例,如果你想根据选择调整大小,你可以在每个对象上添加一个布尔值 mSelected 并更改视图取决于此布尔值)

编辑:然后在 MultiSpinner 中:

删除此:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
            android.R.layout.simple_spinner_item,
            new String[] { spinnerText });
    setAdapter(adapter);

并在微调器中添加新的自定义适配器:

MultiSpinner mMultiSpinner = (MultiSpinner) findViewById(R.id.multispinner);
mMultiSpinner.setAdapter(mCustomArrayAdapter);

As i know, you will have to use a custom adapter, and override the getDropDown and the getView methods. You will be able to customize each item customizing the layout.

Well... words are good, example better, try something like that :

public class CustomStringArrayAdapter extends ArrayAdapter<String>
{
private Activity    myActivity;

public CustomStringArrayAdapter(Context context, int layoutResourceId, int textViewResourceId, List<String> objects, Activity act)
{
    super(context, layoutResourceId, textViewResourceId, objects);
    this.myActivity = act;
}

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

public View getCustomView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = myActivity.getLayoutInflater();
    View row = inflater.inflate(R.layout.spinner_layout, parent, false);
    TextView label = (TextView) row.findViewById(R.id.spinner_textview);
    label.setText(getItem(position));
    LinearLayout layout = (LinearLayout) row.findViewById(R.id.spinner_linear_layout);

    return row;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = myActivity.getLayoutInflater();
    View row = inflater.inflate(R.layout.spinner_layout, parent, false);
    TextView label = (TextView) row.findViewById(R.id.spinner_textview);
    label.setText(getItem(position));

    return row;
    }

}

And the layout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:gravity="center"
    android:background="@drawable/transparent"
    android:id="@+id/spinner_linear_layout">
    <TextView
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:gravity="center"
        android:layout_gravity="center"
        android:textSize="21sp"
        android:singleLine="true"
        android:id="@+id/spinner_textview"
        android:textColor="@color/black" />
</LinearLayout>

(But it's just an sample, if you want to resize depending on the selection, you can add a boolean mSelected on each object and change the view depending on this boolean)

Edit : Then in the MultiSpinner :

Remove this :

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
            android.R.layout.simple_spinner_item,
            new String[] { spinnerText });
    setAdapter(adapter);

and add the new custom adapter in the spinner :

MultiSpinner mMultiSpinner = (MultiSpinner) findViewById(R.id.multispinner);
mMultiSpinner.setAdapter(mCustomArrayAdapter);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文