首先,我對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);
}
}
}
在p問題是我無法將從我的PowerComponent.getView()
返回的View
添加到我目前的View
。
您正在使用'pc.getView',但在您的代碼中我沒有看到任何您初始化它的地方!另外,你是否得到任何錯誤或什麼?你可以提供logcat – Mithun
'PowerComponent pc = new PowerComponent();'它應該是什麼,更新問題以反映這一點。不,沒有錯誤,因爲我被困在如何繼續。 – Emz
您可以嘗試'view.addView(pc.getView(getActivity()));'因爲您需要將'PowerComponent'附加到'Fragment',其中'View'充滿了片段 – Mithun