2016-12-30 5 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

関連する問題