【问题标题】:Android, custom view class disorders weight of linear layoutAndroid,自定义视图类错乱权重的线性布局
【发布时间】:2013-03-30 03:25:54
【问题描述】:

Linear Layout and weight in Android 可能重复

嗨,

我有一个奇怪的问题。我设计了一个自定义小部件,它具有三个按钮的线性布局。它的代码是这样的:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3" >

    <ToggleButton
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/tbBrowse"
        android:background="@drawable/btn_tg_browse"
        android:textOn=""
        android:textOff=""
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:checked="false"
        android:gravity="bottom"/>

    <ToggleButton
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/tbHotSpot"
        android:background="@drawable/btn_tg_hotspot"
        android:textOn=""
        android:textOff=""
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:checked="true"
        android:gravity="bottom"/>

    <ToggleButton
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/tbMatches"
        android:background="@drawable/btn_tg_matches"
        android:textOn=""
        android:textOff=""
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:checked="false"
        android:gravity="bottom"/>

</LinearLayout>

它的外观和感觉是这样的,看起来还可以:

我已将此小部件添加到我的班级 (android:id="@+id/Footer"):

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/White">

    <view
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.belldigital.widget.HeaderBar"
        android:id="@+id/Header"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <view
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.belldigital.widget.FooterBar"
        android:id="@+id/Footer"
        android:layout_alignParentBottom="true" />

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/Header"
        android:layout_above="@id/Footer"/>


</RelativeLayout>

由于“视图”声明中没有定义重力,我希望看到与第一张图片相同的外观和感觉,但我在模拟器和真实设备中看到以下图片。

你怎么看?如果我基于相对布局重新设计我的小部件可能没问题。但是,我认为当前的设计还可以,但为什么“视图”小部件会扰乱我的排列? 任何意见/建议将不胜感激。

【问题讨论】:

    标签: android android-linearlayout android-custom-view


    【解决方案1】:

    我将上面的“视图”更改为关注,然后我的问题神奇地解决了:)

    <view
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            class="com.belldigital.widget.FooterBar"
            android:id="@+id/Footer"
            android:layout_alignParentBottom="true"
            android:orientation="vertical"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多