2016-12-30 3 views
0

HorizontalScrollView을 사용하여 TextViews을 화면 위로 밀어냅니다. 내 질문은 이제 각각의 너비를 전화 화면의 너비와 일치시키는 방법이있는 경우 사용자가 사용하는 전화와 독립적입니다.보기의 너비를 모든 표시 크기와 같게 설정하는 방법은 무엇입니까?

장치의 디스플레이 너비를 얻은 다음 내 너비 인 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

관련 문제