2013-10-23 137 views
13

我想將我的LinearLayout放置在ScrollView中。當LinearLayout的高度很小時,它居中居中(見圖#1),但是當LinearLayout的高度大於屏幕的高度時,它表現得很奇怪。我無法看到LinearLayout的頂部(請參閱圖像#2),並且在ScrollView的底部有巨大的填充。我不知道這裏發生了什麼。當LinearLayout中有大量內容時,整個屏幕應該如圖3所示。滾動視圖中的中心內容

image #1
image #2
image #3

這是我的佈局文件:

  <?xml version="1.0" encoding="utf-8"?> 
      <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#cccfff" > 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_vertical" 
        android:layout_margin="28dp" 
        android:background="#ffffff" 
        android:orientation="vertical" 
        android:paddingBottom="40dp" 
        android:paddingLeft="20dp" 
        android:paddingRight="20dp" 
        android:paddingTop="40dp" > 

        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginBottom="16dp" 
         android:src="@drawable/ic_launcher" /> 

        <TextView 
         android:id="@+id/tip_title" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginBottom="12dp" 
         android:text="Title" 
         android:textColor="@color/orange_text" 
         android:textSize="@dimen/font_size_medium" /> 

        <TextView 
         android:id="@+id/tip_description" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Description description..." 
         android:textSize="@dimen/font_size_small" /> 
       </LinearLayout> 

      </ScrollView> 
+0

集'layout_gravity = center'用於爲*的意見集中* –

+0

先生,你應該爲「按節奏,推節奏,推節奏,推節奏」的給予好評 –

回答

21

您應該使用外部水平的LinearLayout

+0

適用於Android 3.0,適用於所有其他verisones? –

-1

嘗試把利潤率和補白的ScrollView

+0

這沒有幫助。首先我的ScrollView不能有邊距。它必須採取整個屏幕。它也不能有填充,因爲這些填充也適用於右側的ScrollView的滾動條,滾動條應該能夠從屏幕的頂部滾動到底部。 – Egis

0

試試這個可滿足您的要求。在Java代碼中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#cccfff" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_margin="10dp" 
     android:background="#ffffff" 
     android:gravity="center_vertical" 
     android:orientation="vertical" 
     android:paddingBottom="40dp" 
     android:paddingLeft="20dp" 
     android:paddingRight="20dp" 
     android:paddingTop="40dp" > 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="16dp" 
      android:src="@drawable/ic_launcher" /> 

     <TextView 
      android:id="@+id/tip_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="12dp" 
      android:text="Title" /> 

     <TextView 
      android:id="@+id/tip_description" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="biruDescription description..." /> 
    </LinearLayout> 

</LinearLayout> 

下一行

TextView tip_description = (TextView) findViewById(R.id.tip_description); 
     tip_description.setMovementMethod(new ScrollingMovementMethod()); 

這將滾動你的描述的內容,而整個佈局。要看到

<ImageView 
      android:focusable="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="16dp" 
      android:src="@drawable/ic_launcher" /> 
+0

當tip_description文本短時,LinearLayout不會垂直居中。 – Egis

+0

我編輯帖子請檢查 –

+0

這是迄今爲止最好的解決方案,但我希望整個LinearLayout可滾動不只是其中的TextView。我一直在Google上搜索一個小時,現在我有一個印象,那就是不可能擁有我想要的東西。哦,好吧...... – Egis

0
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:gravity="center" 
      android:padding="10dp" 
      android:background="#cccfff"> 

      <ScrollView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       > 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_vertical" 
        android:layout_margin="28dp" 
        android:background="#ffffff" 
        android:orientation="vertical" 
        android:paddingBottom="40dp" 
        android:paddingLeft="20dp" 
        android:paddingRight="20dp" 
        android:paddingTop="40dp" > 

        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginBottom="16dp" 
         android:src="@drawable/ic_launcher" /> 

        <TextView 
         android:id="@+id/tip_title" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginBottom="12dp" 
         android:text="Title" 
         android:textColor="@color/orange_text" 
         android:textSize="@dimen/font_size_medium" /> 

        <TextView 
         android:id="@+id/tip_description" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Description description..." 
         android:textSize="@dimen/font_size_small" /> 
       </LinearLayout> 

      </ScrollView> 
</LinearLayout> 
+0

不幸的是,它不起作用。 – Egis

2

將焦點設置你的最頂端的UI組件。它在相同的情況下爲我工作。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:orientation="horizontal" > 
    <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 
     <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center_horizontal" > 
      <TextView 
        android:text="@string/your_text" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 
     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 
-1

也許還不清楚佈局是如何工作的。在你的情況下,你需要設置一個外部佈局來填充父項,並將重力設置爲中心,內部有一個佈局高度爲wrap_content的滾動視圖,以及佈局高度爲wrap_content的內部佈局。然後,如果外部佈局較小,則它將居中滾動視圖。

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center"> 

    <ScrollView 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

     <!-- Content here --> 

     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 
21

您可以使用android:fillViewport="true"來居中內容。事情是這樣的:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 
     <!-- content --> 
</ScrollView> 
+0

這是一個更好的解決方案。它不涉及在層次結構中創建無用的LinearLayout。 –

+0

短而精美的解決方案...謝謝你... !!! –

+0

非常好!'layout_gravity'以LinearLayout爲中心,導致它隱藏工具欄下的內容。這一個工作! – kirtan403