1

私はこれに関する以前の回答の山を調べ、動作しているとマークされたコードを試しましたが、これまでのところ、runtmeで作成しているTextViewが画面を左から右に完全に埋めることを納得させることはできません。通常のトーストと同じように、両側にマージンを追加したいと思います。その後、シェイプにドロップシャドウを追加できます。

これが私のフォームレイアウトです。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rel_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/hello_world"
        android:textColor="@color/holobrightblue"
        android:textSize="48sp"
        android:textStyle="bold"
        tools:context=".MainActivity" />

</RelativeLayout>

..そして私のコード

textView = new TextView(m_Context);
RoundRectShape rs = new RoundRectShape(new float[] { 10, 10, 10, 10, 10, 10, 10, 10 }, null, null);

ShapeDrawable sd =  new ShapeDrawable(rs);
sd.setAlpha(m_opacity);
textView.setBackgroundDrawable(sd);             
textView.setTextColor(m_txtcolor);  
textView.setText(toasttitle+"\n"+toastmessage);
textView.setBackgroundColor(Color.BLACK);
textView.setPadding(10,10,10,10);

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);


lp.setMargins(60, 0, 60, 0);          
textView.setLayoutParams(lp);

toastView = new Toast(m_Context);
toastView.setView(textView);
toastView.setDuration(m_toastlen);
toastView.setGravity(m_screengravity, 0,0);
toastView.show();

前述のように、他のソリューションから試したことは、テキストビューがすべての水平方向のスペースを埋めることを納得させるようには見えません。

その形などを取り除いてみました...

何か案は?

4

2 に答える 2

4

TextView を含むホルダー RelativeLayout が必要になります。

次に、ホルダーの重力を中央に設定できます

RelativeLayout head2 = new RelativeLayout(this);
head2.setId(++myid);
RelativeLayout.LayoutParams head2Params = new RelativeLayout.LayoutParams(
        LayoutParams.FILL_PARENT, 25);
head2Params.addRule(RelativeLayout.BELOW, head1.getId());
head2.setLayoutParams(head2Params);
head2.setBackgroundColor(Color.CYAN);

次に、TextViewを作成します

textView = new TextView(m_Context);
RoundRectShape rs = new RoundRectShape(new float[] { 10, 10, 10, 10, 10, 10, 10, 10 }, null, null);

ShapeDrawable sd =  new ShapeDrawable(rs);
sd.setAlpha(m_opacity);
textView.setBackgroundDrawable(sd);             
textView.setTextColor(m_txtcolor);  
textView.setText(toasttitle+"\n"+toastmessage);
textView.setBackgroundColor(Color.BLACK);
textView.setPadding(10,10,10,10);

LinearLayout.LayoutParams lp = new
   LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                                          ^^^^^^^^^^^^
                                          ^^^^^^^^^^^^MUST  EDIT
lp.setMargins(60, 0, 60, 0);          
textView.setLayoutParams(lp);

次に、 Textview をホルダー relativeLayout に Center Gravity で追加します

head2.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
head2.addView(textView);

それを示す

toastView = new Toast(m_Context);
toastView.setView(textView);
toastView.setDuration(m_toastlen);
toastView.setGravity(m_screengravity, 0,0);
于 2012-08-01T11:13:20.640 に答える
-1

これは、テキストビュー (テキスト) を特定のマージンに設定する簡単なコードです。

LayoutParams lp = new LayoutParams(text.getWidth(), text.getHeight());
        lp.setMargins(5, 5, 0, 5);
        text.setLayoutParams(lp);

setMargins() のパラメータは次のとおりです。

LayoutParams.setMargins(int left,int top , int right,int bottom)

setLayoutParams は LayoutParams の Object をパラメーターとして受け取ります

于 2014-01-02T12:08:51.240 に答える