如果使用权重,Android自定义切换小部件宽度和高度会反转

时间:2015-03-20 14:11:01

标签: android xml user-interface android-layout widget

我对自制的自定义小部件有一个奇怪的问题...所以,我正在扩展Switch小部件,如果我设置宽度和高度以匹配父级(MATCH_PARENT),并且父级是使用重量设置,宽度和高度交换。它使用不使用权重的最后一个父...这是让你明白的xml。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginLeft="60dp"
        android:weightSum="12" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginRight="20dp"
            android:layout_weight="1"
            android:background="@drawable/border"
            android:orientation="vertical"
            android:weightSum="15" >

            <View
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" >
            </View>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_weight="14" >

                <TextView
                    android:id="@+id/switch_1"
                    style="@style/TextAppearance.My.Switch"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:text="@string/switch_one_text" />

                <com.example.MySwitch
                    android:id="@+id/button_1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:thumb="@drawable/switch_thumb"
                    android:track="@drawable/switch_track" />
            </RelativeLayout>

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

因此,在不查看MySwitch小部件代码的情况下,我可以从onMeasure方法中获取并看到宽度和高度是不使用权重的LinearLayout的宽度和高度(只是权重和,即第二个LinearLayout标记来自顶端)。我的自定义开关将在创建开关组件时使用它们(因为它应该更容易而不是手动设置这些值,这就是权重和match_parent的整点)。无论如何,我需要使用权重,因为布局会发生很大变化。我给你的例子是一个缩短版本只是一个FYI。获得正确的高度和宽度的任何帮助都会很棒。哦,API版本是18.谢谢。

请注意,给定的宽度和高度是相反的。因此,如果您在布局编辑器(在Eclipse中说)中查看此内容,您会看到这一点。

0 个答案:

没有答案
相关问题