2013-10-23 7 views
13

Chcę wyśrodkować mój LinearLayout w ScrollView. Kiedy wysokość LinearLayout jest mała, jest wyśrodkowana w porządku (patrz obrazek # 1), ale kiedy wysokość LinearLayout jest większa niż wysokość ekranu, zachowuje się dziwnie. Nie widzę góry LinearLayout (patrz obrazek # 2), a na dole ScrollView jest olbrzymi padding. Nie wiem, co się tutaj dzieje. Kiedy w LinearLayout jest dużo treści, cały ekran powinien wyglądać jak na obrazku nr 3.Wyśrodkuj zawartość w widoku przewijania

image #1
image #2
image #3

Oto mój plik układ:

  <?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

zestaw 'layout_gravity = center' za poglądy zostać skoncentrowane * * –

+0

Ty, Panie, zasługują na upvote dla "Push tempo, tempo naciskać, naciskać tempo wcisnąć tempo" –

Odpowiedz

21

należy użyć zewnętrznego LinearLayout poziomą. To działa dla mnie w tej samej sytuacji.

<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> 
+0

działa na Android 3.0, działa na wszystkich innych Verisones? –

-1

Spróbuj umieścić margines i podkładki do ScrollView

+0

To nie pomaga. Przede wszystkim mój ScrollView nie może mieć marginesów. Musi zająć cały ekran. Również nie może mieć podkładek, ponieważ te wyściółki są również stosowane do paska przewijania ScrollView po prawej stronie, a pasek przewijania powinien być w stanie przewijać od góry do dołu ekranu. – Egis

0

Spróbuj może zaspokoić swoje wymagania.

<?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> 

Jeszcze jedna linia w kodzie java

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

To będzie przewijać treść opisu raczej cały układ.

+0

LinearLayout nie jest wyśrodkowany w pionie, gdy tekst tip_description jest krótki. – Egis

+0

Edytuję post proszę sprawdzić –

+0

Cóż, jest to najlepsze rozwiązanie do tej pory, ale chciałbym, aby cały LinearLayout można było przewijać nie tylko wewnątrz TextView. Przez godzinę jeździłem w Google, a teraz mam wrażenie, że nie można mieć tego, czego chcę. No cóż ... – 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

Niestety, to nie działa. – Egis

2

Ustaw ostrość na górnym składnik najbardziej UI, które chcesz zobaczyć

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

Być może nie jest jasne, jak układ działa. W twoim przypadku musisz ustawić zewnętrzny układ, który wypełnia rodzic i ma ustawiony grawitacyjnie środek, wewnątrz znajduje się widok przewijania z wysokością składu wrap_content i wewnętrzny układ z wysokością zawijania wrap_content również. Następnie układ zewnętrzny wyśrodkuje widok przewijania, jeśli jest mniejszy.

<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

Możesz użyć android:fillViewport="true", aby wyśrodkować zawartość. Coś takiego:

<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

To jest lepsze rozwiązanie. Nie wymaga to tworzenia bezużytecznego LinearLayout w hierarchii. –

+0

Krótkie i ładne rozwiązanie ... dziękuję ... !!! –

+0

Doskonały!'layout_gravity' do wyśrodkowania w LinearLayout spowodowało, że ukrywa zawartość poniżej paska narzędzi. Ten zadziałał! – kirtan403

Powiązane problemy