【问题标题】:spinner in android xml layoutandroid xml布局中的微调器
【发布时间】:2016-05-07 01:53:35
【问题描述】:

我正在尝试为 Android 应用程序设计布局。 这是xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/nameFB"
    android:layout_gravity="end" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="end">

        <Spinner

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:text="@string/type_string" />

        <EditText
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/yearTextName"
            android:background="@drawable/edit_text_style"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:text="@string/year_string" />

        <EditText
            android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/creatorTextName"
            android:background="@drawable/edit_text_style"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:text="@string/creator_string" />

        <EditText
            android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/nameTextBox"
            android:background="@drawable/edit_text_style"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:text="@string/name_string" />


    </LinearLayout>

    <ListView
        android:id="@+id/lv_search_results"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>
</LinearLayout>
</LinearLayout>

它看起来像这样:

但是当我为微调器添加一个 id 值时:

<Spinner
            android:id="@+id/a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

由于某种原因,它破坏了布局:

有人知道为什么会这样吗?我唯一要更改的是 id 的值,而不是图形。 我该如何解决?或者如何重新排列 xml,以便文本框和微调器在文本视图中可见(可能在两行中)?我试图一个接一个地放入线性布局并将小部件放入其中,但每次看起来都像第二张图片。

【问题讨论】:

    标签: android xml android-layout spinner


    【解决方案1】:

    我仍在寻找这种行为背后的原因。

    但目前,您可以使用以下代码进行布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/nameFB"/>
    
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <Spinner
                    android:id="@+id/adsa"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"/>
    
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="type"
                    android:layout_weight="1"/>
    
                <EditText
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:inputType="textPersonName"
                    android:ems="10"
                    android:id="@+id/yearTextName"
                    android:layout_weight="1"/>
    
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="year"
                    android:layout_weight="1"/>
    
                <EditText
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:inputType="textPersonName"
                    android:ems="10"
                    android:id="@+id/creatorTextName"
                    android:layout_weight="1"/>
    
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="craetor"
                    android:layout_weight="1"/>
    
                <EditText
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:inputType="textPersonName"
                    android:ems="10"
                    android:id="@+id/nameTextBox"
                    android:layout_weight="1"/>
    
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="name"
                    android:layout_weight="1"/>
    
        </LinearLayout>
        <ListView
            android:id="@+id/lv_search_results"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
        </ListView>
    </LinearLayout>
    

    您可以根据需要修改视图的权重。

    【讨论】:

      【解决方案2】:

      我强烈建议您在这种情况下更改您的设计。微调器保留在单行中,其余控件保留在第二行中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-01
        相关资源
        最近更新 更多