Android:按钮显示在屏幕底部

时间:2013-07-16 12:47:22

标签: 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 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 - activity_audio_record_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>

谢谢,

亨利

1 个答案:

答案 0 :(得分:1)

如果你真的需要它们动态创建,那么我们可以帮助你。但是,看起来您每次都在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>

Button移除您的LinearLayoutonCreate()作品并更改

setContentView(ll);

setContentView(R.layout.activity_audio_record_test);

您现在可以通过3种不同的方式设置onClick。一种方法是在使用Button初始化buttonName.setOnClickListener( new OnclickListener(){...时执行此操作。另一种方式,有时IMO更容易和更清洁,是通过向android:onClick="someFunction"添加Button来在xml中执行它,然后在Java中有一个函数

public void someFunction(View v)
{
    // place code to run here when that button was clicked
}

对另一个做同样的事情。您也可以使用相同的功能并打开按钮id。如果你想要我可以链接到一个例子

相关问题