简体   繁体   English

将微调器中的图像设置为Gravity.Center

[英]Set image in spinner to Gravity.Center

I have a spinner with images as items, the problem is that they're pushed to the left. 我有一个将图像作为项目的微调器,问题是它们被推到左侧。 A common problem with spinners i think. 我认为微调器是一个常见的问题。 I've worked this out before but only with textviews, not images. 我以前已经解决了这个问题,但是只使用了textviews,而不是图像。 How can I move the images to the center of the dropdown spinner menu? 如何将图像移到下拉微调器菜单的中心?

Here's my custom spinner xml: 这是我的自定义微调器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="50dp"
 android:orientation="horizontal"
 android:gravity="center"
 android:layout_gravity="center"
 >

    <ImageView
     android:id="@+id/icon"
     android:layout_width="35dp"
     android:layout_height="35dp"
     android:scaleType="fitStart"
     android:gravity="center"
     android:layout_gravity="center"
     android:src="@drawable/icon"
     />

</LinearLayout>

I've tried setting it to center in every possible way in xml (as you can see), but can't get it working.. 我尝试将其设置为以xml的所有可能方式居中(如您所见),但无法使其正常工作。

I've tried this in java: 我已经在Java中尝试过此方法:

        ...
        @Override
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub

            ((ImageView) getCustomView(position, convertView, parent)).setGravity(Gravity.CENTER);

        }
      ...

but i get an error on setGravity saying: The method setGravity(int) is undefined for the type ImageView 但是我在setGravity上收到一条错误消息:方法setGravity(int)对于ImageView类型未定义

what to do ?? 该怎么办 ??

THanks 谢谢

I think your problem is that the LinearLayout's width is wrap_content, meaning that it will only be as wide as the image. 我认为您的问题是LinearLayout的宽度是wrap_content,这意味着它只会和图像一样宽。 Therefor it is centered. 因此,它居中。 Try setting the layout_width="match_parent" 尝试设置layout_width =“ match_parent”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM