首先,我对Android很新。可能有些愚蠢的错误,请纠正你看到的错误。
我遇到的问题是在现有视图中添加视图。您可以按一个按钮来减少值。它可以工作,如果我将它们保存在单独的片段中,但是我想要一个片段来处理所有片段。问题出现在Playfield
中,在onCreateView
方法中。
addView(pc.getView(getActivity())); // this I know is the error, but absolutely no clue of how to solve it.
这就是我想要完成的。我想直接将该视图添加到当前视图中。
我尝试添加另一个小部件并为其提供此视图的属性,但是失败了。
fragment_playfield.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:text="@string/fire_button" />
</RelativeLayout>
球场
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class Playfield extends Fragment
{
private View view;
private PowerComponent pc = new PowerComponent();;
private final static int BUTTON_FIRE = R.id.button1;
private Button buttonFire;
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_playfield, container, false);
addView(pc.getView(getActivity())); // this I know is the error, but absolutely no clue of how to solve it.
buttonFire = (Button) view.findViewById(BUTTON_FIRE);
buttonFire.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
fire ();
}
});
return view;
}
private void fire () {
if (pc.firePower()) {
System.exit(0);
}
}
}
PowerComponent
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
public class PowerComponent
{
private PowerComponentView view;
private static final int POWER_MAX = 100;
private static int power = POWER_MAX;
public View getView (Activity activity) {
view = new PowerComponentView(activity);
return view;
}
public boolean firePower () {
if (power < 5) {
return false;
}
power -= 5;
view.invalidate();
return true;
}
private class PowerComponentView extends View
{
/**
* Power GUI
*/
private final Paint paint = new Paint();
private final int GUI_BAR_X = 100;
private final int GUI_BAR_Y = 10;
private final int GUI_BAR_HEIGHT = 30;
private final int GUI_BAR_WIDTH_MAX = 200;
private final Rect powerFrame = new Rect (GUI_BAR_X-1, GUI_BAR_Y-1, GUI_BAR_X + GUI_BAR_WIDTH_MAX+1, GUI_BAR_HEIGHT+1);
private final Rect powerBar = new Rect (GUI_BAR_X, GUI_BAR_Y, GUI_BAR_X + GUI_BAR_WIDTH_MAX, GUI_BAR_HEIGHT);
/**
*
* @param context
*/
public PowerComponentView (Context context) {
super (context);
}
@Override
public void onDraw (Canvas canvas) {
super.onDraw(canvas);
// Frame
paint.setColor (Color.BLACK);
canvas.drawRect (powerFrame, paint);
// Bar
paint.setColor (Color.rgb (255, 255, 0));
powerBar.right = GUI_BAR_X + (int)(((double)power/(double)POWER_MAX) * (double)GUI_BAR_WIDTH_MAX);
canvas.drawRect (powerBar, paint);
}
}
}
问题是我无法将View
返回的PowerComponent.getView()
添加到我当前的View
。
答案 0 :(得分:1)
Fragment类没有addView()方法。 addView是ViewGroup类的一种方法。使用:
container.addView()