【问题标题】:How to place 2 buttons at the same position in my layout如何在我的布局中将 2 个按钮放在同一位置
【发布时间】:2013-04-19 04:35:04
【问题描述】:

我的 xml 布局中有如下所示的“我的表格”行:

<TableRow 
          android:layout_marginTop="10dp"
            >

            <SeekBar
                android:id="@+id/seekBar1"
                android:layout_width="256dp"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/titlename"
                android:layout_="@+id/playbtn"
                android:layout_marginTop="4dp"
               />
             <Button
            android:id="@+id/playbtn"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:background="@drawable/play"
            android:layout_marginBottom="3dp"/>

        <Button
            android:id="@+id/pausebtn"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_toTopOf="@+id/playbtn"
            android:layout_alignParentBottom="true"
            android:background="@drawable/pause"
            android:layout_marginBottom="3dp"/>
        </TableRow>

我的输出如下,

我的要求是在布局中的相同位置显示播放暂停按钮?

有人可以帮忙吗?

【问题讨论】:

    标签: android android-layout android-intent android-widget


    【解决方案1】:

    使用FrameLayout 并将一个按钮放在另一个按钮上

    这样

    <FrameLayout ... >
          <Button
                android:id="@+id/playbtn"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/play"/>
    
            <Button
                android:id="@+id/pausebtn"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/pause"/>
    
    </FrameLayout>
    

    这会将暂停按钮置于播放按钮之上。根据需要制作必要的按钮visibleinvisible

    【讨论】:

    • 感谢您的快速回复,framelayout 运行良好,但我想将搜索栏放在播放/暂停按钮旁边,在我的表格行中?
    • 将包含按钮的Framelayout 放在Seekbar 的右侧。
    【解决方案2】:

    尝试使用此布局。使用 LinearLayout 和 FrameLayout 的组合来达到预期的效果。

    <TableRow 
          android:layout_marginTop="10dp">
    <LinearLayout 
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">
            <SeekBar
                android:id="@+id/seekBar1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weight="1"
                android:layout_alignLeft="@+id/titlename"
                android:layout_="@+id/playbtn"
                android:layout_marginTop="4dp"
               />
    
     <FrameLayout 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" >
        <Button
            android:id="@+id/playbtn"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/play"/>
        <Button
            android:id="@+id/pausebtn"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/pause"/>
    
      </FrameLayout>
    </LinearLayout>
    </TableRow>
    

    【讨论】:

      【解决方案3】:

      一次试试这个:

      <TableRow
              android:id="@+id/tableRow1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1" >
      
              <SeekBar
                  android:id="@+id/seekBar1"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight=".8" />
      
              <Button
                  android:id="@+id/play"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight=".2"
                 />
          </TableRow>
      

      主要活动

      private boolean playing = false;
          Button play;
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              play=(Button) findViewById(R.id.play);
              play.setBackgroundResource(R.drawable.pause);
      
              play.setOnClickListener(new OnClickListener() {
      
                  @Override
                  public void onClick(View arg0) {
                      // TODO Auto-generated method stub
                      if(playing){
                          playing = false;
                          play.setBackgroundResource(R.drawable.pause);
                      }
                      else {
                          playing = true;
                          play.setBackgroundResource(R.drawable.play);
                      }
                  }
              });
      
          }
      

      【讨论】:

        【解决方案4】:

        【讨论】:

          【解决方案5】:

          您不需要为它创建两个按钮。你可以用单身做到这一点。我创建了一个示例程序。我发现这是一种更清洁的方法。

          MainActivity 布局

          <LinearLayout 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:orientation="vertical"
              android:gravity="center">
          
              <Button
                  android:id="@+id/buttonPlayPause"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
          
          </LinearLayout>
          

          这里是 MainActivity

          package in.live.homam.imagebuttonexample;
          
          import android.app.Activity;
          import android.os.Bundle;
          import android.util.Log;
          import android.view.View;
          import android.widget.Button;
          
          public class MainActivity extends Activity {
          
              private Button buttonPlayPause;
          
              private static final int resourceIdPlay = R.drawable.play;
              private static final int resourceIdPause = R.drawable.pause;
              private int resourceIdButton = resourceIdPlay;
          
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
          
                  buttonPlayPause = (Button) findViewById(R.id.buttonPlayPause);
                  buttonPlayPause.setBackgroundResource(resourceIdButton);
                  buttonPlayPause.setOnClickListener(new View.OnClickListener() {
          
                      @Override
                      public void onClick(View v) {
                          if(resourceIdButton == resourceIdPlay) {
                              resourceIdButton = resourceIdPause;
                              Log.d("Tag", "Playing");
                          }
                          else {
                              resourceIdButton = resourceIdPlay;
                              Log.d("Tag", "Paused");
                          }
                          buttonPlayPause.setBackgroundResource(resourceIdButton);
          
                      }
                  });
              }
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-07-24
            • 2016-06-24
            • 1970-01-01
            • 1970-01-01
            • 2021-07-28
            • 1970-01-01
            • 2016-10-09
            相关资源
            最近更新 更多