【问题标题】:Problems with custom spinner alignment and width自定义微调器对齐和宽度的问题
【发布时间】:2012-03-06 12:28:54
【问题描述】:

我使用自己的 ProjectSpinnerAdapter 创建了一个自定义微调器。我还有另一个类,它只是扩展了布局,但尚未实现,所以仍在使用标准微调器。

我似乎无法让我的自定义微调器以与标准微调器相同的方式对齐。

当项目的长度比表格单元格宽时,我也会遇到问题,如下所示。如果选项超过一定长度,有没有办法截断它们? - 这似乎不是解决此问题的正确方法,但我无法直接设置小部件的宽度,因为这是通过表格布局控制的。*

**已修复,请参阅更新*

截图

标准微调器与其旁边的 Button 完美对齐(它们都包含在同一个 TableRow 中)

布局

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1" >

<TableRow>

    <Button
        android:id="@+id/selectButton"
        android:text="@string/show" />

    <Spinner android:id="@+id/projectSpinner" />
</TableRow>

spinner_list_item.xml(使用 maxLength)

<?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="wrap_content"
>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/title_text_view"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:maxLength="15"
          android:gravity="center"/>


</LinearLayout>

更新

问题是微调器项目的布局,在我的自定义布局 spinner_list_item.xml 中,我指定了一个布局,我只需要 textview

spinner_list_item.xml(使用 maxLength)

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/title_text_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:maxLength="15" />

【问题讨论】:

    标签: android android-layout android-widget android-spinner


    【解决方案1】:

    如果我可能会问,您为什么选择 TableLayout?我认为RelativeLayoutlayout_toRightOf layout_below 等也可以实现同样的效果。或者LinearLayoutweightSumgravity 每个孩子都有一个layout_weight

    我不会给你一个改变你整个布局的解决方案;),但我测试了这个,这似乎对我有用。

    <TableRow>
    
        <Button
            android:id="@+id/selectButton"
            android:maxLength="10"
            android:text="sdadadasdfffffffffffffffffffffffff"/>
    
        <Spinner
            android:id="@+id/projectSpinner"
            android:ellipsize="end"
            android:lines="1"
            android:scrollHorizontally="true" />
    </TableRow>
    

    希望这是可行的,它是比设置固定字符串长度更好的方法

    编辑

    我不确定您为什么要以现在的方式填充微调器,但您可以尝试:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/title_text_view"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerInParent="true"
              android:maxLength="15"/>
    
    
    </RelativeLayout>
    

    android:gravity 也是错误的,必须是 android:layout_gravity(但这只是 LinearLayout

    上次编辑:D

    尝试使用默认布局

    view = vi.inflate(android.R.layout.spinner_list_item, null);
    

    然后

    ((TextView) view.findViewById(R.id.text1)).setText(title);
    

    【讨论】:

    • 对于宽度问题,这是一个非常简洁的解决方案 - 谢谢。关于对齐问题的任何想法?
    • 好的,所以我在 spinner_item.xml 中为“TextView”指定了“maxLength”属性,并将完整长度留在 spinner_dropdown_item.xml 中,这样就解决了宽度问题。 +1
    • 什么都没有,你能把R.layout.spinner_list_item 的xml 贴出来。也许您的父 TableLayout 应该使用 width:fill_parent 和 height:wrap_content?
    • 更改了表格布局,但仍无法正确排列。
    • 你能把这个布局展示给view = vi.inflate(R.layout.spinner_list_item, null);吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2016-06-07
    相关资源
    最近更新 更多