【问题标题】:Android Switch dosent show in ViewGroupAndroid Switch 不显示在 ViewGroup 中
【发布时间】:2017-01-25 15:37:32
【问题描述】:

我想创建一个这样的小部件:

image 1

但是,显示的是这样的:

image 2

为什么Switch Button 没有显示在ViewGroup 中:

在这种情况下,仅显示 Switch 的文本 "hello"

public class TestView extends ViewGroup {
   ...
   private void init() {
       imageView = new ImageView(getContext());
       imageView.setImageResource(R.drawable.clock_icon);

       aSwitch = new Switch(getContext());
       aSwitch.setText("hello");
       aSwitch.setChecked(true);

       addView(imageView);
       addView(aSwitch);

   }

   @Override
   protected void onSizeChanged(int w, int h, int oldw, int oldh) {
       super.onSizeChanged(w, h, oldw, oldh);
       imageView.layout(0, 50,100, 70);
       aSwitch.layout(50,50,100,70);
   }
...

【问题讨论】:

    标签: android uiswitch android-viewgroup


    【解决方案1】:

    为布局创建资源文件可能更容易:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/clock_icon"/>
        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello"
            android:checked="true"/>
    </LinearLayout>
    

    然后在需要时以编程方式对其进行膨胀:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.your_resource_file, container, false);
    }
    

    【讨论】:

    • 谢谢@Charlie,但我想通过编程来创建它。
    【解决方案2】:

    我不确定ViewGroup.layout(int l, int t, int r, int b) 是否正确定义视图的大小和位置,因为此方法只是绘图视图整体流程的一部分:

    ViewGroup.layout(int l, int t, int r, int b)是布局机制的第二阶段。 (首先是测量) https://developer.android.com/reference/android/view/ViewGroup.html#layout(int, int, int, int)

    但无论如何,您可以尝试拨打View.requestLayout()。当某些内容发生变化而导致此视图的布局无效时调用此函数。
    https://developer.android.com/reference/android/view/View.html#requestLayout()

    唯一的问题是,由于某种原因,它在View.onSizeChanged(int w, int h, int oldw, int oldh) 中不起作用,因此您必须执行以下操作:

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        imageView.layout(0, 50,100, 70);
        aSwitch.layout(50,50,100,70);
        post(new Runnable() {
            @Override
            public void run() {
                imageView.requestLayout();
                aSwitch.requestLayout();
            }
        });
    }
    

    【讨论】:

      【解决方案3】:

      tnx @Gugalo 我用你的描述解决了这个问题。

       @Override
          protected void onSizeChanged(int w, int h, int oldw, int oldh) {
              super.onSizeChanged(w, h, oldw, oldh);
              aSwitch.measure(w,h);
              aSwitch.layout(0, 0,  aSwitch.getMeasuredWidth(), aSwitch.getMeasuredHeight());
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-30
        • 1970-01-01
        相关资源
        最近更新 更多