gpt4 book ai didi

java - 自定义 joystickView 在 xml 中不起作用 - Android

转载 作者:行者123 更新时间:2023-11-30 02:54:08 25 4
gpt4 key购买 nike

我正在尝试为我的项目实现自定义 JoystickView,通过搜索我找到了这个库:https://github.com/zerokol/JoystickView

我已将 jar 添加到 lib 文件夹并将 jar 文件添加到构建路径。问题是当我尝试将它添加到我的屏幕上时它不起作用。我是 android 的新手,现在被困住了。你能告诉我哪里错了吗?

以下代码与链接示例代码相同,除了 xml 文件

我的主要类(class): 包 com.example.joystickdeneme;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

import com.zerokol.views.JoystickView;
import com.zerokol.views.JoystickView.OnJoystickMoveListener;

public class MainActivity extends Activity {



private TextView angleTextView;
private TextView powerTextView;
private TextView directionTextView;
// Importing as others views
private JoystickView joystick;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

angleTextView = (TextView) findViewById(R.id.angleTextView);
powerTextView = (TextView) findViewById(R.id.powerTextView);
directionTextView = (TextView) findViewById(R.id.directionTextView);
// referring as others views
joystick = (JoystickView) findViewById(R.id.joystickView);

// Listener of events, it'll return the angle in graus and power in percents
// return to the direction of the moviment
joystick.setOnJoystickMoveListener(new OnJoystickMoveListener() {
@Override
public void onValueChanged(int angle, int power, int direction) {
angleTextView.setText(" " + String.valueOf(angle) + "°");
powerTextView.setText(" " + String.valueOf(power) + "%");
switch (direction) {
case JoystickView.FRONT:
directionTextView.setText(R.string.front_lab);
break;

case JoystickView.FRONT_RIGHT:
directionTextView.setText(R.string.front_right_lab);
break;

case JoystickView.RIGHT:
directionTextView.setText(R.string.right_lab);
break;

case JoystickView.RIGHT_BOTTOM:
directionTextView.setText(R.string.right_bottom_lab);
break;

case JoystickView.BOTTOM:
directionTextView.setText(R.string.bottom_lab);
break;

case JoystickView.BOTTOM_LEFT:
directionTextView.setText(R.string.bottom_left_lab);
break;

case JoystickView.LEFT:
directionTextView.setText(R.string.left_lab);
break;

case JoystickView.LEFT_FRONT:
directionTextView.setText(R.string.left_front_lab);
break;

default:
directionTextView.setText(R.string.center_lab);
}
}
}, JoystickView.DEFAULT_LOOP_INTERVAL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

和我的 xml 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:id="@+id/angleTextView" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:id="@+id/powerTextView" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:id="@+id/directionTextView" />


<com.example.joystickdeneme

zerokol.views.joystickview=".zerokol.views.joystickview"
android:id="@+id/joystickView"
android:layout_height="wrap_content"
android:layout_width="wrap_content" >
</com.example.joystickdeneme>

</LinearLayout>

最佳答案

我知道我的错误在布局文件中,我称之为操纵杆的地方应该是:<com.zerokol.views.JoystickView android:id="@+id/joystickView" android:layout_width="150dp" android:layout_height="150dp" />

关于java - 自定义 joystickView 在 xml 中不起作用 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23591690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com