2016-12-30 72 views
0

我正在使用HorizontalScrollView在屏幕上滑動TextViews。我現在的問題是,如果有什麼方法可以使每個RelativeLayout的寬度與電話屏幕的寬度相匹配,則與用戶使用的電話無關。如何設置視圖的寬度等於任何顯示?

那麼有沒有辦法像獲取設備的顯示寬度,然後將其設置爲我的RelativeLayout的寬度?還是有更好的方法來做到這一點?

<HorizontalScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginBottom="20dp" 
    android:layout_marginTop="20dp" 
    android:fillViewport="true"> 

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

     <RelativeLayout 
      android:layout_width="320dp" 
      android:layout_height="match_parent" 
      android:background="@android:color/holo_blue_dark"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="" 
       android:textSize="15sp" 
       android:textColor="@android:color/black" 
       android:id="@+id/info" 
       android:layout_margin="5dp" 
       android:textAlignment="center"/> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/info" 
       android:text="0" 
       android:textSize="80dp" 
       android:layout_centerHorizontal="true" 
       android:id="@+id/getrunkeneLiter"/> 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="320dp" 
      android:layout_height="match_parent" 
      android:background="@android:color/holo_green_light"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="" 
       android:textColor="@android:color/black" 
       android:layout_margin="5dp" 
       android:id="@+id/danke" 
       android:textAlignment="center"/> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Feedback verfassen" 
       android:onClick="feedback" 
       android:layout_below="@+id/danke" 
       android:layout_centerHorizontal="true"/> 
     </RelativeLayout> 

    </LinearLayout> 

</HorizontalScrollView> 

回答

1

您可以獲取屏幕大小並以編程方式將其設置爲您的每個視圖。或者,您可以使用加權的LinearLayout並將其大小設置爲屏幕大小的元素數。

您可以使用此函數來獲取屏幕尺寸(然後使用Point的x值):

public static Point getScreenSize(Context context) { 
    final Point size = new Point(); 
    final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
    final Display display = wm.getDefaultDisplay(); 
    display.getSize(size); 
    return size; 
} 

要更新,你需要一個ID添加到他們每個人的相對佈局。例如:

<RelativeLayout 
    android:id="@+id/first_subview" 
    android:layout_width="320dp" 
    android:layout_height="match_parent" 
    android:background="@android:color/holo_blue_dark"> 

然後在你的活動:

private void updateViews() { 
    Point screenSize = getScreenSize(this); 
    updateView(findViewById(R.id.first_subview), screenSize); 
    updateView(findViewById(R.id.second_subview), screenSize); 
    ... 
} 

private void updateView(View view, Point screenSize) { 
    view.getLayoutParams().width = screenSize.x 
} 
+0

嘿嘿,謝謝!如何將relativeLayoutWidth設置爲Point的x值? .-。 – user7342339

+0

我更新了示例以包含設置部分。 –

+0

這只是天才!非常感謝您的幫助,謝謝! – user7342339

相關問題