【问题标题】:Composite widget - what is the preferred way?复合小部件 - 首选方式是什么?
【发布时间】:2012-11-27 18:04:57
【问题描述】:

我想构建可重用的小部件。它应该是标准元素的有序组合。什么是最好的方法。下面是我目前使用的代码示例,但可能会有更优雅的东西。此外,下面的示例在运行时完美运行,但 Eclipse 中的可视化编辑器抛出异常(这对我来说目前不是问题)。有没有推荐的创建复合材料的方法?我应该使用片段吗?

public class MyComposite extends LinearLayout {

  private ImageView m_a1;
  private ImageView m_a2;
  private ImageView m_w1;
  private ImageView m_w2;
  private ImageView m_w3;
  private ImageView m_w4;

  public CallBackSlider(final Context context) {
    this(context, null);
  }

  public CallBackSlider(final Context context, final AttributeSet attrs) {
    super(context, attrs);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.my_composite, this, true);
    setupViewItems();
  }

  private void setupViewItems() {
    m_a1 = (ImageView) findViewById(R.id.A1Img);
    m_w1 = (ImageView) findViewById(R.id.Wave1Img);
    m_w2 = (ImageView) findViewById(R.id.Wave2Img);
    m_w3 = (ImageView) findViewById(R.id.Wave3Img);
    m_w4 = (ImageView) findViewById(R.id.Wave4Img);
    m_a2 = (ImageView) findViewById(R.id.A2Img);
    resetView();
  }

  private void resetView() {
    m_w1.setAlpha(0);
    m_w2.setAlpha(0);
    m_w3.setAlpha(0);
    m_w4.setAlpha(0);
  }
}

布局xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/MyComposite"
  ...  >

  <ImageView
    android:id="@+id/A1Img"
    android:src="@drawable/a1"
    ...
   />

  <ImageView
    android:id="@+id/Wave1Img"
    ...
    android:src="@drawable/wave1" />

  <ImageView
    android:id="@+id/Wave2Img"
    ...
    android:src="@drawable/wave2" />

  <ImageView
    android:id="@+id/Wave3Img"
    ...
    android:src="@drawable/wave3" />

  <ImageView
    android:id="@+id/Wave4Img"
    ...
    android:src="@drawable/wave4" />

<ImageView
    android:id="@+id/EarImg"
    ...
    android:src="@drawable/a2" />

</LinearLayout>

然后你可以像这样在其他布局中使用它:

...
<your.package.MyComposite 
    android:id="@+id/mc1"
    ...       
/>
...

并使用 java 代码以及 MyComposite 类的实例。

【问题讨论】:

    标签: android android-widget android-view eclipse-adt


    【解决方案1】:

    最近引入了嵌套片段(在 SDK 4.1 或 4.2 以及兼容库中)。但我倾向于认为应该使用这些嵌套片段来封装更复杂的 UI 片段,而不仅仅是像你这样的简单小部件。

    自定义小部件的常用方法是覆盖 onFinishInflate 方法。你有一个关于that blog的教程。

    下面是对应的一段代码:

    public class TwoTextWidget extends LinearLayout {
        private TextView textOne;
        private TextView textTwo;
    
        public TwoTextWidget(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void onFinishInflate() {
            super.onFinishInflate();
            ((Activity)getContext()).getLayoutInflater().inflate(R.layout.two_text_component, this);
            setupViewItems();
        }
    
        private void setupViewItems() {
            textOne = (TextView) findViewById(R.id.text_one);
            textTwo = (TextView) findViewById(R.id.text_two);
        }
    
        public void setTextOne(String text) {
            textOne.setText(text);
        }
    
        public void setTextTwo(String text) {
            textTwo.setText(text);
        }
    }
    

    您可以允许使用自定义属性对小部件进行更多自定义。查看that other blog

    【讨论】:

    • 你提到的两个博客我都准备好了。我对覆盖 onFinishInflate() 的 samlpe 代码有一些问题。这是推荐的方式还是只是某人的想法?我也知道自定义属性:stackoverflow.com/questions/5706038/…
    • 那么你知道你需要的一切我猜 :) 我从来没有遇到过关于 onFinishInflate 的任何问题。这是我大部分时间看到的方法。您可以尝试浏览一些 OpenSource 小部件,看看它们是如何做到的。
    【解决方案2】:

    在您的示例中,我认为您需要合并的部件。

    如果您需要更改小部件的标准行为,请对其进行自定义,然后使用您的自定义小部件。

    但是如果你需要一组具有标准行为的视图,合并它们就足够了。

    some_activity.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:orientation="vertical">
    
        // some views
    
        <include layout="@layout/view_part"/>
    
       // probably more views
    
    </LinearLayout>
    

    view_part.xml:

        <merge xmlns:android="http://schemas.android.com/apk/res/android">
    
           <ImageView
           android:id="@+id/A1Img" 
           android:src="@drawable/a1"
           ...
           /> 
    
           <ImageView
           android:id="@+id/Wave1Img"
           ...
           android:src="@drawable/wave1" />
    
           <ImageView
           android:id="@+id/Wave2Img"
           ...
           android:src="@drawable/wave2" />
    
           <ImageView
           android:id="@+id/Wave3Img"
           ...
           android:src="@drawable/wave3" />
    
           <ImageView
           android:id="@+id/Wave4Img"
           ...
           android:src="@drawable/wave4" />
    
           <ImageView
           android:id="@+id/EarImg"
           ...
           android:src="@drawable/a2" />       
    
        </merge>
    

    【讨论】:

    • 我正在谷歌上搜索什么是合并的小部件。你能给我一些好的解释吗?为什么它很好,什么时候应该使用。
    • 例如,您需要一个按钮在单击时始终创建一条 Toast 消息,或者您需要一个具有不同滑动动画的图库。因此,在这种情况下,您可以扩展图库或按钮以创建自定义视图,以便在您的活动中轻松使用它。但是在您的问题中,您需要一组具有标准行为的小部件。所以你需要合并它们并在你的活动中使用它。
    猜你喜欢
    • 2011-05-19
    • 1970-01-01
    • 2011-03-01
    • 2012-09-27
    • 2018-06-15
    • 2011-08-09
    • 2014-12-22
    • 2017-07-16
    • 1970-01-01
    相关资源
    最近更新 更多