我有一个有 3 个视图的主屏幕:顶部有一个“GO”按钮和一个 EditText 栏(都在加权水平线性布局中),下面有一个垂直线性布局。在这个垂直的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>
任何帮助,将不胜感激。