【问题标题】:WheelView not filling parentWheelView 不填充父级
【发布时间】:2013-06-19 22:07:49
【问题描述】:

我正在使用Yuri Kanivets's WheelView control,我发现它没有填充父视图,即使布局 XML 指定它这样做。

这是我的意思的图像:

这是布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00000000">

    <LinearLayout
        android:orientation="vertical"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:background="@drawable/picker_bar">
            <Button
                android:id="@+id/repeatButton"
                android:textColor="#FFF"                
                android:layout_gravity="center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/picker_button_selected"
                android:paddingTop="8dp"
                android:paddingBottom="8dp"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:layout_marginLeft="7dp"
                android:gravity="center"
                android:textSize="16sp"
                android:text="Repeat"/>
            <Button
                android:id="@+id/DelayButton"
                android:layout_gravity="center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/picker_button_unselected"
                android:paddingTop="8dp"
                android:textColor="#FFF"
                android:paddingBottom="8dp"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"  
                android:layout_marginLeft="10dp"  
                android:gravity="center"
                android:textSize="16sp"         
                android:text="Delay"/>
            <Button
                android:id="@+id/doneButton"
                android:layout_marginLeft="10dp"
                android:layout_gravity="center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/picker_button_unselected"
                android:textColor="#FFF"
                android:paddingTop="8dp"
                android:paddingBottom="8dp"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:gravity="center"
                android:textSize="16sp"
                android:text="Done"/>               
        </LinearLayout>

        <include layout="@layout/repeat_view"/>
        <include layout="@layout/delay_view"/>
   </LinearLayout>
</RelativeLayout>

这是重复/延迟视图 XML(它们几乎相同):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/wheelRepeat"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:background="@drawable/layout_bg"
    android:layout_width="fill_parent">
    <LinearLayout 
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_width="fill_parent"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"     
        android:orientation="horizontal">
       <desiderata.ihunt.resources.WheelView 
            android:id="@+id/activeRepeat"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"/>             
       <desiderata.ihunt.resources.WheelView 
            android:id="@+id/minsRepeat"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="2"/>
       <desiderata.ihunt.resources.WheelView 
            android:id="@+id/secsRepeat"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="2"/>                 
    </LinearLayout>
</LinearLayout>

这是我添加视图的方式:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.settings);

            ...

我查看了 WheelView 源代码,但不清楚整个控件的宽度在哪里设置,以及滚轮控件是否也在以某种方式控制父布局的宽度。

这是在最新的 SDK 上运行的。我有什么明显的遗漏吗?

【问题讨论】:

  • 我看不到轮子选择器与您发布的 XML 之间没有任何联系。您是否发布了正确的文件?
  • @Simon 我添加了重复/延迟视图 XML 文件。如果您需要查看其他内容,请告诉我
  • 是否有两种视图,一种用于重复,一种用于延迟?如果是这样,则可能存在 ID 冲突。
  • 在您拥有的所有WheelView 视图中,指定android:layout_width=0dp

标签: android android-layout android-intent android-wheel


【解决方案1】:

包含三个desiderata.ihunt.resources.WheelViewLinearLayoutandroid:layout_width="fill_parent",但 WheelViews 本身有:

android:layout_height="wrap_content"
android:layout_width="wrap_content"

话虽如此,如果我正确理解了这个问题的意思,如果三个轮子的宽度小于parent 的宽度(在这种情况下,LinearLayout 包含三个desiderata.ihunt.resources.WheelView)-那么您必须使用纯色(例如黑色)拉伸填充 LinearLayout 的 Wheels 以隐藏其背后的内容。

【讨论】:

    【解决方案2】:

    g00dy 的替代解决方案(尽管他完全正确)- 如果将每个轮子的宽度设置为 0dp 并设置它们的 layout_weight=1,则 3 将拉伸以占据屏幕的相等部分。如果您不希望它们相等,请将它们设置为您想要的比例。

    【讨论】:

    • 不错的选择!我试试看
    【解决方案3】:
     <LinearLayout 
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_width="fill_parent"
            android:paddingTop="4dp"
            android:weightSum="3"      // add weight sum here 
            android:paddingBottom="4dp"     
            android:orientation="horizontal">
           <desiderata.ihunt.resources.WheelView 
                android:id="@+id/activeRepeat"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"/>             //weights here
           <desiderata.ihunt.resources.WheelView 
                android:id="@+id/minsRepeat"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"/>              //weights here
           <desiderata.ihunt.resources.WheelView 
                android:id="@+id/secsRepeat"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"/>              //weights here    
        </LinearLayout>
    

    像这样改变你的轮子布局

    【讨论】:

      【解决方案4】:

      为您的重复/延迟视图 xml 尝试这样的布局

      <LinearLayout 
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/wheelRepeat"
          android:layout_height="wrap_content"
          android:orientation="vertical" 
          android:background="@drawable/layout_bg"
          android:layout_width="fill_parent">
          <LinearLayout 
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:layout_width="fill_parent"
              android:paddingTop="4dp"
              android:paddingBottom="4dp"     
              android:orientation="horizontal"
              android:weightSum="10">
      
             <desiderata.ihunt.resources.WheelView 
                  android:id="@+id/activeRepeat"
                  android:layout_height="wrap_content"
                  android:layout_width="0dp"
                  android:layout_gravity="center_vertical"
                  android:layout_weight="2"/>             
             <desiderata.ihunt.resources.WheelView 
                  android:id="@+id/minsRepeat"
                  android:layout_height="wrap_content"
                  android:layout_width="0dp"
                  android:layout_gravity="center_vertical"
                  android:layout_weight="4"/>
             <desiderata.ihunt.resources.WheelView 
                  android:id="@+id/secsRepeat"
                  android:layout_height="wrap_content"
                  android:layout_width="0dp"
                  android:layout_gravity="center_vertical"
                  android:layout_weight="4"/>                 
          </LinearLayout>
      </LinearLayout>
      

      这将使小部件填充屏幕宽度,您可以通过调整布局权重来调整宽度比例。如果您不需要小部件在横向模式下填满整个屏幕,您可以使用 wrap content 作为父线性布局的宽度。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-14
        • 1970-01-01
        相关资源
        最近更新 更多