2016-10-12 2 views
0
@Override 
public void onClick(View view) { 
    switch (view.getId()) { 
     case R.id.btnOKButt: 
      displayToast("My text"); 
      startActivity(new Intent(Activity1.this, Activity2.class)); 
      finish(); 
      break; 
    } 
} 
private void displayToast(String s) { 

//デフォルトのトーストビューグループが私はトーストを持っていると思いrelativelayoutはトーストをお持ちの真ん中

Toast toast = Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG); 
    LinearLayout toastLayout = (LinearLayout) toast.getView(); 
    TextView toastTV = (TextView) toastLayout.getChildAt(0); 
    toast.getView().setBackgroundColor(Color.BLACK); 
    toast.setGravity(Gravity.FILL, 0, -250); 
    toastTV.setTextSize(30); 

    toast**strong text**.show(); 
} 

}

あるディスプレイ全体はまだテキストを持つ埋めます背景全体を塗りつぶしても、中央にテキストがあります。しかし、私が 'Gravity.FILL'を使うと、私はいつもトップにテキストを持っています。自分は何をすべきですか?

答えて

0

TextViewのgravity = centerでcreate a custom toast viewにする必要があります。

レイアウトファイル

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/custom_toast_container" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:padding="8dp" 
> 
<TextView android:id="@+id/text_toast" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:text="@string/hello_toast" /> 
</LinearLayout> 

ショートースト

 LayoutInflater inflater = getLayoutInflater(); 
    View layout = inflater.inflate(R.layout.test_toast, 
      (ViewGroup) findViewById(R.id.custom_toast_container)); 

    TextView text = (TextView) layout.findViewById(R.id.text_toast); 
    text.setTextSize(40); 
    Toast toast = new Toast(getApplicationContext()); 
    toast.setGravity(Gravity.FILL, 0, 0); 
    toast.setDuration(Toast.LENGTH_LONG); 
    toast.setView(layout); 
    toast.show(); 
: それはそのようなものになるだろう
関連する問題