Android:使按钮出现在屏幕底部
我有一个类来录制音频笔记,我希望将其实现到现有的活动中,但是本例中的“录制”和“播放”按钮最终会出现在屏幕顶部,并将事情弄得一团糟 在何处以及如何使用以下代码指定按钮的显示属性 JAVA-AudioRecordTest.JAVAAndroid:使按钮出现在屏幕底部,android,android-layout,android-intent,audio-recording,android-audiorecord,Android,Android Layout,Android Intent,Audio Recording,Android Audiorecord,我有一个类来录制音频笔记,我希望将其实现到现有的活动中,但是本例中的“录制”和“播放”按钮最终会出现在屏幕顶部,并将事情弄得一团糟 在何处以及如何使用以下代码指定按钮的显示属性 JAVA-AudioRecordTest.JAVA public class AudioRecordTest extends Activity { private static final String LOG_TAG = "AudioRecordTest"; private static String mFileNam
public class AudioRecordTest extends Activity
{
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName = null;
private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;
private PlayButton mPlayButton = null;
private MediaPlayer mPlayer = null;
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void onPlay(boolean start) {
if (start) {
startPlaying();
} else {
stopPlaying();
}
}
private void startPlaying() {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
private void stopPlaying() {
mPlayer.release();
mPlayer = null;
}
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
class RecordButton extends Button {
boolean mStartRecording = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onRecord(mStartRecording);
if (mStartRecording) {
setText("Stop recording");
} else {
setText("Start recording");
}
mStartRecording = !mStartRecording;
}
};
public RecordButton(Context ctx) {
super(ctx);
setText("Start recording");
setOnClickListener(clicker);
}
}
class PlayButton extends Button {
boolean mStartPlaying = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onPlay(mStartPlaying);
if (mStartPlaying) {
setText("Stop playing");
} else {
setText("Start playing");
}
mStartPlaying = !mStartPlaying;
}
};
public PlayButton(Context ctx) {
super(ctx);
setText("Start playing");
setOnClickListener(clicker);
}
}
public AudioRecordTest() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mRecordButton = new RecordButton(this);
mPlayButton = new PlayButton(this);
setContentView(R.layout.activity_audio_record_test);
}
@Override
public void onPause() {
super.onPause();
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
}
XML-活动\u音频\u录制\u test.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ScrollView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inspection ID" />
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/txtName"
android:inputType="number"
android:maxLength="5"
android:digits="0123456789"
android:singleLine="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text1" />
<EditText
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoText="false"
android:gravity="top|left"
android:lines="4"
android:maxLines="4"
android:minLines="4"
android:scrollbars="vertical"
android:singleLine="false"
android:width="0dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project Ref"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/txtAge"
android:inputType="number"
android:maxLength="5"
android:digits="0123456789"
android:singleLine="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drop Down"
/>
<Spinner
android:id="@+id/spinDept"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnPhotoCamera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Take Image with Camera" />
<Button
android:id="@+id/btnPhotoGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Get Image from Gallery" />
<TextView
android:id="@+id/lblDisplayImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnCancel"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="below_this_text_image_will_be_displayed"
android:textSize="13dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/lblDisplayImage"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:gravity="bottom" >
<!--
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
-->
<ImageView
android:id="@+id/imgDisplayImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="area_where_image_is_to_be_displayed" />
<!-- </ScrollView> -->
</RelativeLayout>
<------------------- RECORD AND PLAY BUTTONS TO GO HERE ------------->
<Button
android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnAddEmp_Click"
android:text="Save Inspection" />
<Button
android:id="@+id/btnCancel"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_below="@+id/btnPhotoGallery"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="Reset/Clear Form Data" />
<TextView
android:id="@+id/txtEmps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number of Inspections on Device " />
</LinearLayout>
</ScrollView>
</LinearLayout>
>
谢谢
henry如果您确实需要动态创建它们,那么我们可以提供帮助。但是,每次在
onCreate()
中似乎都在无条件地创建它们,因此将它们放在xml中会容易得多
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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=".AudioRecordTest" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<LinearLayout
android:id="@+id/btnLL"
android:layout_width="wrap_content"
android:layuout_height="wrap_content"
android:layout_alignParentBottom="true"> <!-- This property will put them at the bottom of the screen -->
<Button
android:id="@+id/recBtn"
android:layout_width="wrap_content"
android:layuout_height="wrap_content"
/>
<Button
android:id="@+id/playBtn"
android:layout_width="wrap_content"
android:layuout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>
到
现在,您可以通过三种不同的方式之一设置onClick
s。一种方法是在使用buttonName.setOnClickListener(新的OnclickListener(){…
)初始化按钮时执行此操作。另一种方法是在xml中添加android:onClick=“someFunction”
到按钮中,然后在Java中创建一个函数
public void someFunction(View v)
{
// place code to run here when that button was clicked
}
对另一个做同样的操作。您也可以对它们使用相同的功能,并打开按钮id
。如果您需要,我可以链接到一个示例任何小部件的定位通常在布局文件(xml)中完成。您能显示xml文件中按钮所在的位置吗?@bofredo按钮是通过编程方式创建的。通常,我会使用一个按钮和一个onClick
来启动一个意图。然而,在这种情况下,按钮会“神奇地”出现在我认为的@Override public void onCreate(捆绑冰柱)中{…
我已经发布了一个答案,可以告诉你如何用xml来做。这里没有必要用Java来做。它们看起来并不是“神奇的”。它们是通过编程创建的。创建布局参数时,重力设置为底部,并在ADDVIEW PASS中输入该参数。哈哈,我通常将它们放在XML中,但在我复制的代码中,它们来自@Override public void onCreate(捆绑冰柱){…
因此,如果我添加到XML,那么我可能需要删除最前面提到的onCreate?从onCreate()中删除按钮
和LinearLayout
引用
。然后查看我答案的更新。有点……按钮没问题,我tweeked,但功能上它们什么都不做……我的onclick是什么?这是不是因为我缺少查看我的更新。你只需按正常方式设置它们,这可能是几种不同方式中的一种。我们可以用不同的方式查看吗……我想在上面放置它们s按钮
setContentView(R.layout.activity_audio_record_test);
public void someFunction(View v)
{
// place code to run here when that button was clicked
}