2013-03-06 271 views
0

我有一個主要佈局在我的問答遊戲中,還有3個嵌套佈局,一個在另一個下面,垂直佈局。在第一個問題中,我有一個問題的文本視圖,中間有四個垂直排列的按鈕。現在,當我有一行問題時,一切都很好,但只要問題進入兩行,我的按鈕就會向下移動一行。如何防止這一點?我希望我的問題下面的所有內容都是固定的,靜態的,不要移動。如何防止佈局移動?

<?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="@drawable/background" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="404dp" 
     android:orientation="vertical" 
     android:weightSum="2" 
     > 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_weight="1" > 

    <TextView 
     android:id="@+id/tvPitanje" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="20dp" 
     android:gravity="center" 
     android:text="@string/tvPitanje" 
     android:textSize="22sp" /> 
    </LinearLayout> 

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

    <Button 
     android:id="@+id/bOdgovor1" 
     android:layout_width="260dp" 
     android:layout_height="60dp" 
     android:layout_gravity="center" 
     android:layout_marginTop="10dp" 
     android:background="@drawable/buttons" 
     android:text="@string/bOdgovor1" 
     android:textSize="26sp" /> 

    <Button 
     android:id="@+id/bOdgovor2" 
     android:layout_width="260dp" 
     android:layout_height="60dp" 
     android:layout_gravity="center" 
     android:layout_marginTop="20dp" 
     android:background="@drawable/buttons" 
     android:text="@string/bOdgovor2" 
     android:textSize="26sp" /> 

    <Button 
     android:id="@+id/bOdgovor3" 
     android:layout_width="260dp" 
     android:layout_height="60dp" 
     android:layout_gravity="center" 
     android:layout_marginTop="20dp" 
     android:background="@drawable/buttons" 
     android:text="@string/bOdgovor3" 
     android:textSize="26sp" /> 

    <Button 
     android:id="@+id/bOdgovor4" 
     android:layout_width="260dp" 
     android:layout_height="60dp" 
     android:layout_gravity="center" 
     android:layout_marginTop="20dp" 
     android:background="@drawable/buttons" 
     android:text="@string/bOdgovor4" 
     android:textSize="26sp" /> 

    </LinearLayout> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" > 

     <Button 
      android:id="@+id/bIzlazIzKviza" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_marginLeft="15dp" 
      android:background="@drawable/buttons" 
      android:text="@string/bIzlazKviz" 
      android:textSize="23sp" /> 

     </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/tvCountdown" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="50dp" 
      android:layout_marginTop="5dp" 
      android:gravity="center" 
      android:text="@string/tvCountdown" 
      android:textColor="#ff0000" 
      android:textSize="29sp" /> 

     <TextView 
      android:id="@+id/tvSkor" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="30dp" 
      android:layout_marginTop="5dp" 
      android:gravity="center" 
      android:text="@string/tvSkor" 
      android:textSize="23sp" /> 

     </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" > 

     <TextView 
      android:id="@+id/tvBrojPitanja" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_marginLeft="30dp" 
      android:text="1" 
      android:textSize="32sp" /> 

     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 
+0

請提供XML佈局 – QVDev 2013-03-06 16:43:45

+0

在這裏你去交配。整個XML。 – user2083882 2013-03-06 17:15:26

回答

0

過了一會兒,但在這裏我們去,也有這個問題,我將指出這裏他們兩個若干解決方案,可能是最用戶友好:

  1. 使用一切滾動視圖,恕我直言,這是用戶最友好的解決方案。
  2. 使用scrollview僅用於具有定義高度的文本和按鈕,然後將下面的文本和按鈕設置爲靜態,然後使用相對佈局。
  3. 字幕文本視圖

我會把在這裏,有一些佈局重構的第一個選項包括,第二個我不會推薦使用,把很簡單的實現,如果你有例子之一:

<?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="@android:color/background_light" 
       android:orientation="vertical"> 

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

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

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

       <TextView 
         android:id="@+id/tvPitanje" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center" 
         android:layout_marginLeft="5dp" 
         android:layout_marginRight="5dp" 
         android:layout_marginTop="20dp" 
         android:gravity="center" 
         android:text="long text that will automaticly scroll and make sure all content will be displayed right in the scrollview this is the best case scenario" 
         android:textSize="22sp"/> 

       <Button 
         android:id="@+id/bOdgovor1" 
         android:layout_width="260dp" 
         android:layout_height="60dp" 
         android:layout_gravity="center" 
         android:layout_marginTop="10dp" 
         android:text="one" 
         android:textSize="26sp"/> 

       <Button 
         android:id="@+id/bOdgovor2" 
         android:layout_width="260dp" 
         android:layout_height="60dp" 
         android:layout_gravity="center" 
         android:layout_marginTop="20dp" 
         android:text="two" 
         android:textSize="26sp"/> 

       <Button 
         android:id="@+id/bOdgovor3" 
         android:layout_width="260dp" 
         android:layout_height="60dp" 
         android:layout_gravity="center" 
         android:layout_marginTop="20dp" 
         android:text="three" 
         android:textSize="26sp"/> 

       <Button 
         android:id="@+id/bOdgovor4" 
         android:layout_width="260dp" 
         android:layout_height="60dp" 
         android:layout_gravity="center" 
         android:layout_marginTop="20dp" 
         android:text="four" 
         android:textSize="26sp"/> 

      </LinearLayout> 

      <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="fill_parent" 
        android:orientation="horizontal"> 

       <LinearLayout 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:layout_weight="1"> 

        <Button 
          android:id="@+id/bIzlazIzKviza" 
          style="?android:attr/buttonStyleSmall" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_gravity="center" 
          android:layout_marginLeft="15dp" 
          android:text="left" 
          android:textSize="23sp"/> 

       </LinearLayout> 

       <LinearLayout 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:layout_weight="1" 
         android:orientation="vertical"> 

        <TextView 
          android:id="@+id/tvCountdown" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_marginLeft="50dp" 
          android:layout_marginTop="5dp" 
          android:gravity="center" 
          android:text="next" 
          android:textColor="#ff0000" 
          android:textSize="29sp"/> 

        <TextView 
          android:id="@+id/tvSkor" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_marginLeft="30dp" 
          android:layout_marginTop="5dp" 
          android:gravity="center" 
          android:text="middle" 
          android:textSize="23sp"/> 

       </LinearLayout> 

       <LinearLayout 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:layout_weight="1"> 

        <TextView 
          android:id="@+id/tvBrojPitanja" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_gravity="center" 
          android:layout_marginLeft="30dp" 
          android:text="right" 
          android:textSize="32sp"/> 

       </LinearLayout> 

      </LinearLayout> 

     </LinearLayout> 

    </ScrollView> 

</LinearLayout> 

跑馬燈解決過去那麼好

android:singleLine="true" 
android:ellipsize="marquee" 
android:marqueeRepeatLimit ="marquee_forever" 
android:scrollHorizontally="true" 
android:focusable="true" 
android:focusableInTouchMode="true" 
+0

我得到的錯誤:引起:java.lang.IllegalStateException:ScrollView只能託管一個直接的孩子 – user2083882 2013-03-07 12:02:35

+0

對不起,我似乎忘了粘貼一些代碼:),我編輯了上面的答案。現在它應該工作。僅供參考滾動視圖只能包含一個直接視圖。而在之前的版本中,我發佈的版本已經增加了。它現在應該工作。 – QVDev 2013-03-07 12:08:58

+0

仍然是一樣的錯誤。 :(( – user2083882 2013-03-07 16:50:18