Android - 从自定义布局获取子视图的句柄

3
我创建了一个继承自LinearLayout的Java类,如下所示。
         public class CustomLinear extends LinearLayout{

        Context context;

         public CustomLinear(Context context) {
        super(context);
          this.context=context;

         ViewFlipper viewFlipper=new ViewFlipper(context);
         viewFlipper.setLayoutParams(this.getLayoutParams());
        this.addView(viewFlipper);

        }

我将自定义的布局包含在我的Main.xml中,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res/com.example.android.apis"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">



              <com.homevito.customlayout.CustomLinear
            android:id="@+id/one"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:background="@drawable/alerts"
            android:orientation="horizontal" />


</LinearLayout>

在我的MainActivity中,我使用main.xml作为内容视图,如下所示:

     public class MainActivity extends Activity {



         /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
            setContentView(R.layout.main);


        }
    }

我希望你能够从MainActivity中处理viewFlipper。我尝试为viewFlipper设置Id,但还是没用...

我该如何处理viewflipper... 提前致谢。


public News(Context context) 应该更改为 public CustomLinear(Context context) - Vipul
很抱歉那个打字错误。 - Vivekanand
2个回答

2
尝试下面的代码片段。
class CustomLinear extends LinearLayout {

    private Context context;

    public CustomLinear(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

        ViewFlipper viewFlipper = new ViewFlipper(context);
        viewFlipper.setId(1234);
        viewFlipper.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        this.addView(viewFlipper);

    }

在 onCreate() 方法中

CustomLinear customLinear = (CustomLinear) findViewById(R.id.one);

ViewFlipper flipper = (ViewFlipper) customLinear.findViewById(1234);

好的,Shah,我试过了...但是我没有使用customLinear的对象...我会立刻尝试这个。 - Vivekanand

1
首先,如果您在xml布局中使用自定义组件,则需要实现第二个构造函数(接受ContextAttributeSet参数)。 其次,您可以为您的ViewFlipper添加一个getter方法:
public class CustomLinear extends LinearLayout{

        Context context;
        private ViewFlipper mFlipper;

        public CustomLinear(Context context, AttributeSet attrs) {
             super(context, attrs);
             this.context=context;
             mFlipper = new ViewFlipper(context);
             mFlipper.setLayoutParams(this.getLayoutParams());
             this.addView(mFlipper);
        }

      // the getter method
      public ViewFlipper getTheFlipper() {
           return mFlipper;
      }

然后在您的活动中,您可以简单地执行以下操作:

setContentView(R.layout.main);
CustomLinear cl = (CustomLinear) findViewById(R.id.one);
ViewFlipper flipperReference = cl.getTheFlipper();

如果你不介意的话,能否告诉我构造函数中第三个属性的必要性。 - Vivekanand
@vivek 抱歉,构造函数除了 Context 之外还需要一个 AttributeSet。这是必需的,因为您在 xml 布局中使用自定义组件,而这些 AttributeSet 来自 xml 属性(例如 android:background="@drawable/alerts")。当您直接在代码中实例化自定义视图时,将使用带有 Context 的构造函数。 - user
噢,是的,我明白了......在你之前的帖子中,第三个属性(int i)让我有些困惑.... :D - Vivekanand

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接