2014-01-30 77 views
0

好了,所以我有這個問題:enter image description here如何將這個textView垂直居中?

我要瘋了,因爲我一直在努力,現在找到這個問題的答案爲約3小時。 無論如何,我想對齊第一個textView(如圖所示)垂直居中。 我正在編程創建這些textViews(不需要問爲什麼,我只需要)。 代碼:

private View formTextViewUnits(String units) { 
    TextView textViewUnits = new TextView(getActivity()); 
    textViewUnits.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); 
    textViewUnits.setText(units); 
    textViewUnits.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL); 
    textViewUnits.setTextColor(Color.parseColor("#e9e9e9")); 
    textViewUnits.setTextSize(TypedValue.COMPLEX_UNIT_DIP, getResources().getDimension(R.dimen.main_stripe_small_text)); 

    return textViewUnits; 
} 

private View formTextViewAmount(Float amount) { 
    TextView textViewAmount = new TextView(getActivity()); 
    textViewAmount.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
    textViewAmount.setText(amount.toString()); 
    textViewAmount.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL); 
    textViewAmount.setTextColor(Color.parseColor("#3ebce6")); 
    textViewAmount.setTextSize(TypedValue.COMPLEX_UNIT_DIP, getResources().getDimension(R.dimen.main_stripe_big_text)); 

    return textViewAmount; 
} 

//THIS IS THE FAULTY TEXTVIEW 
private View formTextViewMessage(String message) { 
    TextView textViewMessage = new TextView(getActivity()); 
    textViewMessage.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
    textViewMessage.setText(message); 
    textViewMessage.setGravity(Gravity.CENTER_VERTICAL); 
    textViewMessage.setTextColor(Color.parseColor("#e9e9e9")); 
    textViewMessage.setTextSize(TypedValue.COMPLEX_UNIT_DIP, getResources().getDimension(R.dimen.main_stripe_small_text)); 

    return textViewMessage; 
} 

這裏是(使用tableLayout)我的片段錶行吧:

<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingTop="@dimen/main_stripe_padding_top" 
    android:paddingBottom="@dimen/main_stripe_padding_bottom" 
    android:layout_marginBottom="@dimen/main_stripe_margin_bottom" 
    android:gravity="center_vertical" 
    android:layout_gravity="center_vertical" 
    android:background="#424242"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:layout_span="2" 
     android:gravity="right" 
     android:paddingLeft="@dimen/main_stripe_text_padding_left" 
     android:paddingRight="@dimen/main_stripe_text_padding_right" 
     android:orientation="horizontal" 
     android:id="@+id/linearLayoutStripe"> 
    </LinearLayout> 

    </TableRow> 

任何想法?

回答

3

TextView.setGravity(...)將文本的重力設置在您的TextView的範圍內。由於所涉及的TextView的垂直高度爲wrap_content,因此視圖的邊界始終是內容的高度。

如果將TextView的高度設置爲match_parent,並且保持現有重力設置,則TextView中的文本應該在其父佈局中垂直居中顯示。

+0

謝謝你的迴應。我試過:'textViewMessage.setLayoutParams(LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));' 但是,這是發生了什麼(抱歉的外部鏈接):https:// dl-web。 dropbox.com/get/Camera%20Uploads/2014-01-30%2016.09.39.png?_subject_uid=99305133&w=AAAGpTu_QjGqACwiwExyXOMiGEkGl2scSSLDUYrccAartw – user3178137

+0

嗯,你的鏈接不適合我。 – Yjay

+0

通過在fragment中爲linear_layout添加重力參數來修復它:) – user3178137