如何将文本右/左设置为我的customAndroidView?

时间:2013-11-12 10:21:35

标签: java android android-layout layout

我想创建两行

TextView .... CustomeSwitvchView

CustomeSwitchView ... TextView

但是,使用此xml不会产生预期的顺序。

这两行只显示与右边对齐的CustomeSwitchView,没有显示文字。

    <LinearLayout style="@style/settingsSection">


        <RelativeLayout
            android:id="@+id/settingsSwitchLayoutRtl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            <com.me.settings.SettingsSwitchView
                    style="@style/settingsSelection" android:background="@drawable/item_selector_single"
                android:id="@+id/settingsSwitchRtl" />

            <TextView
                android:id="@+id/switch_text"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@id/settingsSwitchRtl"
                android:layout_centerVertical="true"
                android:textColor="#00FF00"
                android:text="My Settings1" />
      </RelativeLayout>

        <RelativeLayout
             android:id="@+id/settingsSwitchLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            <com.me.settings.SettingsSwitchView
                    style="@style/settingsSelection" android:background="@drawable/item_selector_single"
                    android:id="@+id/settingsSwitch" />


                <TextView
                android:id="@+id/switch_text"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/settingsSwitch"
                android:layout_centerVertical="true"
                android:textColor="#00FF00"
                android:text="My Settings2" />


        </RelativeLayout>

    </LinearLayout>

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:0)

试试这个:

<LinearLayout style="@style/settingsSection">


        <RelativeLayout
            android:id="@+id/settingsSwitchLayoutRtl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <com.me.settings.SettingsSwitchView
                    style="@style/settingsSelection" android:background="@drawable/item_selector_single"
                    android:layout_alignParentTop="true"
                android:id="@+id/settingsSwitchRtl" />

            <TextView
                android:id="@+id/switch_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@id/settingsSwitchRtl"
                android:layout_centerVertical="true"
                android:gravity="center_horizontal"
                android:textColor="#00FF00"
                android:text="My Settings1" />
      </RelativeLayout>

        <RelativeLayout
             android:id="@+id/settingsSwitchLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <com.me.settings.SettingsSwitchView
                    style="@style/settingsSelection" android:background="@drawable/item_selector_single"
                    android:id="@+id/settingsSwitch" />

            <TextView
                android:id="@+id/switch_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/settingsSwitch"
                android:layout_centerVertical="true"
                android:gravity="center_horizontal"
                android:textColor="#00FF00"
                android:text="My Settings2" />
        </RelativeLayout>

    </LinearLayout>

答案 1 :(得分:0)

试试这个..

<LinearLayout style="@style/settingsSection">


        <RelativeLayout
            android:id="@+id/settingsSwitchLayoutRtl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            <com.me.settings.SettingsSwitchView
                  style="@style/settingsSelection"
                  android:background="@drawable/item_selector_single"
                  android:layout_alignParentRight="true"
                  android:paddingLeft="85dp"
                  android:id="@+id/settingsSwitchRtl" />

            <TextView
                android:id="@+id/switch_text"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_alignParentLeft="true"
                android:textColor="#00FF00"
                android:text="My Settings1" />
      </RelativeLayout>

        <RelativeLayout
             android:id="@+id/settingsSwitchLayout"
                android:layout_width="wrap_content"
                android:layout_below="@+id/settingsSwitchLayoutRtl"
                android:layout_height="wrap_content">

            <com.me.settings.SettingsSwitchView
                    style="@style/settingsSelection" 
                    android:layout_alignParentLeft="true"
                    android:paddingRight="85dp"
                    android:background="@drawable/item_selector_single"
                    android:id="@+id/settingsSwitch" />


                <TextView
                android:id="@+id/switch_text"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_alignParentRight="true"
                android:textColor="#00FF00"
                android:text="My Settings2" />


        </RelativeLayout>

    </LinearLayout>

答案 2 :(得分:0)

我不确定,但你可以尝试一下

<LinearLayout style="@style/settingsSection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation = "vertical">


        <RelativeLayout
            android:id="@+id/settingsSwitchLayoutRtl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <com.me.settings.SettingsSwitchView
                    style="@style/settingsSelection" android:background="@drawable/item_selector_single"
                 android:layout_alignParentRight="true"
                android:id="@+id/settingsSwitchRtl" />

            <TextView
                android:id="@+id/switch_text"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@id/settingsSwitchRtl"
                android:layout_centerVertical="true"
                android:textColor="#00FF00"
                android:text="My Settings1" />
      </RelativeLayout>

        <RelativeLayout
             android:id="@+id/settingsSwitchLayout"
                android:layout_width="match_parent"
                android:layout_below="@+id/settingsSwitchLayoutRtl"
                android:layout_height="wrap_content">

            <com.me.settings.SettingsSwitchView
                    style="@style/settingsSelection" android:background="@drawable/item_selector_single"
                    android:layout_alignParentLeft="true"
                    android:id="@+id/settingsSwitch" />


                <TextView
                android:id="@+id/switch_text"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/settingsSwitch"
                android:layout_centerVertical="true"
                android:textColor="#00FF00"
                android:text="My Settings2" />


        </RelativeLayout>

    </LinearLayout>