2017-09-23 50 views
0

我们尝试了两种方法将自定义Snackbar(1)显示为伪装对话框,该对话框不会移动到屏幕底部但它并不会忽略当前的活动视图只是使其不透明。我知道它为什么在屏幕的中心,但我无法将其移动到底部。 (2)接下来是一个接管整个屏幕的视图,因为它是一个新的内容视图,我猜测它将取消当前的活动视图,但它位于屏幕的底部。 所以我的问题是如何使用设计编号1并将对话框移动到屏幕底部? 第二个问题如何停止设计编号2中的新视图从关闭当前活动的视图?经过仔细阅读和小小的思考和极端测试,我不认为这是可能的!我已经在下面发布了我的两种方法的代码。 XML文件使用相对布局作为基本容器。XML文件中的自定义视图Snackbar将不会显示屏幕底部

public void seeSB(){ 

     setContentView(R.layout.custom_snackbar); 
     // Line of Code above shows XML file 
     // Line of code tested but no control over the "viewMyLayout" 
     //LayoutInflater inflater = LayoutInflater.from(ListActivity.this); 
     //final View viewMyLayout = inflater.inflate(R.layout.custom_snackbar, null); 
     //viewMyLayout.setEnabled(true); 

     Button btnAB = (Button) findViewById(R.id.btnAB); 
     btnAB.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       // viewMyLayout.setEnabled(false); 
       // Line above does not function 
       // CODE BELOW WORKS BUT FAR FROM elegant 
       setContentView(R.layout.activity_list); 
       //Intent intent = new Intent(ListActivity.this, ListActivity.class); 
       //startActivity(intent); 

       Toast.makeText(getApplicationContext(), "I WAS Clicked", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

    } 

    public void displaySB(){ 

     final Dialog openSnack = new Dialog(context); 
     openSnack.setContentView(R.layout.custom_snackbar); 
     Button btnAB = (Button)openSnack.findViewById(R.id.btnAB); 
     TextView tvSB =(TextView)openSnack.findViewById(R.id.tvSB); 
     //Dialog dialog = new Dialog(ListActivity.this); 
     //dialog.setContentView(Bottom); 

     // if YES delete Master Password from TABLE_MPW 
     btnAB.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       openSnack.dismiss(); 
       Toast.makeText(getApplicationContext(), "I WAS Clicked", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     openSnack.show(); 
    } 
+0

所以你想要一个小吃店在活动的底部没有解散活动。对? –

+0

@AlokKumarVerma是的,我可以使用design.widget.CoordinatorLayout完成此操作,但是当我使用该XML文件进行不同大小设备的样式设置时,创建自定义XML文件将提供更少的代码 –

回答

1

这远远功能在我的书,因为该方法设计只有一个自定义小吃吧来看看,所以你需要了解如何有多个固定的自定义小吃店工作。一个建议可能是在你的父视图中有多个子视图,并调用你想要的子视图。我只会发布我添加到父级XML文件的子视图,以及不太实际的动态方法来实现,在这种情况下通过点击按钮来实现。为了在真实的应用程序中工作,代码将需要从某种方法或事件中调用。 你可能会考虑多个视图的switch语句? ? ?

注意相对布局有其可见性设置为在启动

<RelativeLayout 
    android:id="@+id/hold_snackbar" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/color_Black" 
    android:visibility="gone" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/tvSB" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp" 
     android:layout_marginTop="10dp" 
     android:paddingBottom="10dp" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp" 
     android:paddingTop="10dp" 
     android:text="@string/snackbar_text" 
     android:textColor="@color/color_Yellow" 
     android:textSize="18sp" 
     android:textStyle="bold" /> 

    <Button 
     android:id="@+id/btnAB" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="350dp" 
     android:layout_marginTop="5dp" 
     android:background="@color/color_Transparent" 
     android:focusable="false" 
     android:text="@string/snackbar_action" 
     android:textColor="@color/color_Red" 
     android:textSize="18sp" 
     android:textStyle="bold" /> 

</RelativeLayout> 

注意查看subViewGroup声明GONE当活动开始

View subViewGroup; 

     public void makeSB(View view) { 
     subViewGroup = findViewById(R.id.hold_snackbar); 
     subViewGroup.setVisibility(View.VISIBLE); 
     seeSB(); 
    } 

     public void seeSB(){ 

      Button btnAB = (Button) findViewById(R.id.btnAB); 
      btnAB.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       subViewGroup.setVisibility(View.GONE); 
       Toast.makeText(getApplicationContext(), "I WAS Clicked", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

倒数计时器关闭一个小吃吧,没有动作按钮

public void makeCDT(View view) { 

    cdt = new CountDownTimer(5000, 100) { 
     // 5 sec 5000,100 
     // 10 sec 10000,100 
     @Override 
     public void onTick(long secsUntilFinished) { 
      etCPW.setText(String.valueOf(secsUntilFinished/1000)); 
      //etCPW.setText("seconds remaining: " + secsUntilFinished/1000); 
      subViewGroup = findViewById(R.id.SB_NO_ACTION); 
      subViewGroup.setVisibility(View.VISIBLE); 
     } 

     @Override 
     public void onFinish() { 
      etCPW.setText("Counter Done"); 
      subViewGroup.setVisibility(View.GONE); 

      if(cdt!=null){ 
       cdt.cancel(); 
      } 
     } 
    }; 
    cdt.start(); 

    } 
+0

@James_Duh解决了没有“动作”按钮的小吃店仅仅附加一个CountDownTimer,在你设置它的时间结束后,我将很少使用计时器,所以这是一个有趣的项目增强,我将把代码添加到我的答案中 – Grendel