2013-03-21 2 views
5

보기를 확장하는 맞춤 클래스와 몇 개의 버튼으로 상대적 레이아웃을 사용하려고합니다.Android - XML ​​레이아웃과 프로그래밍 방식

는 현재이 함께 하드 코딩 (, 분명히 나는 ​​아직 충분히 냉각 아니에요 이미지가 아닌 링크를 게시 용서)

http://imgur.com/B5MtdJ7

: 이것은 내가 결국은 같이 할 것입니다 DP의 높이 (아래 XML 참조),이 두 가지 문제가 있습니다 :

  • 그것은 단지 내 넥서스 7 화면에 허용보고,
  • 모두 사용자 정의보기 '의 onDraw이 충족 다른 장치 HOD 여전히 높이 캔버스를 제공하고, 내가 글 머리 기호 2와 일치하는 것 같다 화면 전체를 차지하기 위해 각 사용자 지정보기는 시도를 wrap_content하는 layout_height를 설정하려고하면 정확히 장치

의 해상도와 일치하는지 폭 ,하지만 분명히 내가 원하는 것은 아니다.

두 가지 사용자 지정보기가있는 상대 레이아웃은 그림과 똑같지 만 앉아있는 화면의 크기에 맞게 조정되며 사용자 지정보기 캔버스는 실제 크기가 얼마나 큰지를 실제로 알고 있습니다. 화면의 일부입니다. 어떻게해야합니까?

편집 : "vs 프로그래밍 방식"인 이유는 측정 값을 무시하는 것이 좋지 않을 것이라고 생각하지만 XML과 상호 작용하는 방법을 모릅니다. 차라리 한 곳에서 레이아웃 정의를 사용하고 싶습니다.

내 XML은 다음과 같습니다 :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context=".TrajectoryView" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.5" > 

    <com.sportsim.virtualcricket.view.SideProfileView 
     android:id="@+id/side_profile_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
    </com.sportsim.virtualcricket.view.SideProfileView> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.5" > 

    <com.sportsim.virtualcricket.view.BirdsEyeView 
     android:id="@+id/birds_eye_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.25" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.5" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.5" 
     android:text="Button" /> 
</LinearLayout> 

답변

1

이가있는 LinearLayout과 가중치를 사용하여 매우 쉽게 될 것입니다. 아래 예를 들었지만 지금은 테스트 할 수 없습니다. 그것은 어떤 방향을 제공해야합니다.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context=".TrajectoryView" > 

<com.sportsim.virtualcricket.view.SideProfileView 
    android:id="@+id/side_profile_view" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.5"/> 

<com.sportsim.virtualcricket.view.BirdsEyeView 
    android:id="@+id/birds_eye_view" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.25" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.25" 
    android:orientation="horizontal"> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="0.5" 
    android:text="Button" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="0.5" 
    android:text="Button" /> 

</LinearLayout> 
</LinearLayout> 
+0

감사합니다. 집에 돌아 가면 고맙겠습니다. 이것은 버튼의 상태를 유지할 것인가? (즉, 각각 화면의 너비의 반을 차지하고 서로 옆에 있습니까?) 이것은 단지 테스트 활동이기 때문에 큰 문제는 아니지만 이걸로 할 수있는 걸 배우기. – Imran

+0

좋아, 버튼이 나란히 놓 이도록 수정하겠습니다. 이제 괜찮을거야. 다시 말하지만, 테스트하지 않았고 일반적으로 중첩 된 가중치는 성능에 좋지 않습니다. 버튼 위치를 설정하는 데 사용 된 LinearLayout을 RelativeLayout으로 대체 할 수 있습니다. –

+0

아아, 예. RelativeLayout의 소리가 꽤 좋아 보입니다. - 건배 남자 : D – Imran