膨胀的LinearLayout不会出现在另一个LinearLayout中

时间:2013-09-12 00:57:11

标签: android android-linearlayout android-xml dynamically-generated layout-inflater

我有一个包含3个视图的主屏幕: 顶部的“GO”按钮和EditText条(全部在加权水平LinearLayout中),下面是一个垂直LinearLayout。在这个垂直的LinearLayout(current_actions_queue)中,我要添加ActionHolder对象。

ActionHolder对象扩展了LinearLayout。在此类中,视图从xml中膨胀。 xml的根是另一个 LinaearLayout,里面装满了自己的好东西。

当用户点击GO按钮时,实例化的ActionHolder被添加到current_actions_queue。但没有任何表现。我已经调试了一天以上,我仍然无法弄清楚我做错了什么!

以下是当用户点击GO时发生的事情:

   /**
    * Begins the action chosen by the user. Places the action in a view in the
    * main layout.
    * 
    * @param view
    */
   public void beginNewAction(View view) {
      EditText actionToBegin = (EditText) findViewById(R.id.action_to_begin);
      String actionName = actionToBegin.getText().toString();
      if (actionName.length() == 0)
         return;
      Action action = user.getDictionary().getAction(actionName.toUpperCase());

      if (currentActionsQueue.contains(action)) {
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         builder.setTitle("Action already started!");
         AlertDialog dialog = builder.create();
         dialog.show();
         return;
      }
      if (currentActionsQueue.size() == ActiveMenuActivity.MULTITASK_LIMIT) {
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         builder.setTitle("Multitask limit reached!");
         AlertDialog dialog = builder.create();
         dialog.show();
         return;
      }

      currentActionsQueue.add(action);

      ActionHolder holder = new ActionHolder(this);
      holder.initiate(action);
      LinearLayout currentActionsFrame = (LinearLayout) this.findViewById(R.id.current_action_queue_view);
      currentActionsFrame.addView(holder);
   }

这是ActionHolder类:

public class ActionHolder extends LinearLayout {

   private Action action;
   private String timer;
   public static final int ACTION_TITLE = 0, ACTION_TIMER = 1,
         PAUSEANDPLAY_BTN = 2, FINISH_BTN = 3;
   private View view;



   public ActionHolder(Context context) {
      super(context);
   }



   public ActionHolder(Context context, AttributeSet attr) {
      super(context, attr);
   }



   public ActionHolder(Context context, AttributeSet attr, int defStyle) {
      super(context, attr, defStyle);
   }



   public void initiate(Action input) {
      this.setOrientation(LinearLayout.VERTICAL);
      this.setLayoutParams(new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
      action = input;
      LayoutInflater inflater = LayoutInflater.from(getContext());
      view = inflater.inflate(R.layout.action_holder_layout, this, true);

      TextView actionTitle = (TextView) view
            .findViewById(com.tonimiko.mochi_bean.R.id.action_holder_title);
      actionTitle.setText(action.getActionName());
      actionTitle.setId(ActionHolder.ACTION_TITLE);

      TextView actionTimer = (TextView) view
            .findViewById(R.id.action_holder_timer);
      actionTimer.setId(ActionHolder.ACTION_TIMER);

      Button pauseBtn = (Button) view
            .findViewById(com.tonimiko.mochi_bean.R.id.pause_and_play_timer_btn);
      pauseBtn.setId(ActionHolder.PAUSEANDPLAY_BTN);

      Button finishBtn = (Button) view
            .findViewById(com.tonimiko.mochi_bean.R.id.finish_activity_button);
      finishBtn.setId(ActionHolder.FINISH_BTN);
   }



   @Override
   protected void onLayout(boolean changed, int l, int t, int r, int b) {
      // TODO Auto-generated method stub

   }

}

以下是每个ActionHolder中要膨胀的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/test_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/layout_margins"
    android:background="#90A0AA"
    android:orientation="vertical"
    android:padding="@dimen/layout_margins" >

    <LinearLayout
        android:id="@+id/action_holder_titlebar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="3" >

        <TextView
            android:id="@+id/action_holder_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="Action Title"
            android:textSize="@dimen/action_holder_title_size" />

        <TextView
            android:id="@+id/action_holder_timer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:text="0:00" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/action_holder_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/pause_and_play_timer_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Pause" />

        <Button
            android:id="@+id/finish_activity_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Finish" />

    </LinearLayout>

</LinearLayout>

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

我注意到在你的ActionHolder类中,onLayout有一个空的定义。这可能是问题所在。在定义中添加对super.onLayout(changed, l, t, r, b)的调用,或从子类中删除定义。希望这会有所帮助。