2015-05-27 18 views
5

Đây là bài đăng đầu tiên của tôi ở đây. Tôi vừa học lập trình Android và gặp vấn đề này. Tôi đã tìm kiếm một chút và tìm thấy một số giải pháp nhưng tôi không thể làm cho mã của tôi hoạt động. Vì vậy, tôi muốn biết phần nào của mã hoặc triển khai của tôi là sai.Cách bố trí tỷ lệ bố trí Android bất kể kích thước màn hình và hướng

Vì vậy, vấn đề là tôi muốn tạo bố cục sẽ giữ nguyên tỷ lệ tương tự trên các kích thước và hướng màn hình khác nhau. Dưới đây là mã của tôi: "Bạn cần ít nhất 10 tiếng để đăng hình ảnh"

activity_main.xml

<LinearLayout 
 
    xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:layout_width="fill_parent" 
 
    android:layout_height="fill_parent" 
 
    android:baselineAligned="false" 
 
    android:orientation="vertical"> 
 

 
    <LinearLayout 
 
     android:layout_width="fill_parent" 
 
     android:layout_height="150dp" > 
 
     <TextView 
 
      android:layout_weight="1" 
 
      android:layout_width="0dp" 
 
      android:layout_height="fill_parent" 
 
      android:gravity="left" 
 
      android:id="@+id/textView1"/> 
 
     <TextView 
 
      android:layout_weight="1" 
 
      android:layout_width="0dp" 
 
      android:layout_height="fill_parent" 
 
      android:gravity="right" 
 
      android:id="@+id/textView2"/> 
 
    </LinearLayout> 
 

 
    <LinearLayout 
 
     android:layout_weight="1" 
 
     android:layout_width="fill_parent" 
 
     android:layout_height="fill_parent" > 
 
     <TextView 
 
      android:layout_weight="1" 
 
      android:layout_width="0dp" 
 
      android:layout_height="fill_parent" 
 
      android:id="@+id/textView3"/> 
 
    </LinearLayout> 
 

 
    <LinearLayout 
 
     android:layout_weight="1" 
 
     android:layout_width="fill_parent" 
 
     android:layout_height="fill_parent" > 
 
     <TextView 
 
      android:layout_weight="1" 
 
      android:layout_width="0dp" 
 
      android:layout_height="fill_parent" 
 
      android:gravity="left" 
 
      android:id="@+id/textView4"/> 
 

 
     <TextView 
 
      android:layout_weight="1" 
 
      android:layout_width="0dp" 
 
      android:layout_height="fill_parent" 
 
      android:gravity="center" 
 
      android:id="@+id/textView5"/> 
 

 
     <TextView 
 
      android:layout_weight="1" 
 
      android:layout_width="0dp" 
 
      android:layout_height="fill_parent" 
 
      android:gravity="right" 
 
      android:id="@+id/textView6"/> 
 
    </LinearLayout> 
 

 
    <SeekBar 
 
     android:layout_width="fill_parent" 
 
     android:layout_height="wrap_content" 
 
     android:id="@+id/seekBar" 
 
     android:max="255"/> 
 
</LinearLayout>

tôi không thể đăng ảnh chụp màn hình vào lúc này, bởi vì nó cho biết

Vì vậy, tôi muốn chia màn hình theo 3 phần theo chiều dọc và chia phần trên theo 2 theo chiều ngang và phần dưới theo 3 theo chiều ngang và duy trì tỷ lệ của chúng bất kể kích thước màn hình, độ phân giải và hướng.

Phần duy nhất không hoạt động là khối trên cùng, trên mã tôi đặt nó thành android:layout_height="150dp" vì cách bố trí bằng cách nào đó bị hỏng nếu tôi đặt chúng thành android:layout_height="fill_parent". Nhưng điều này không làm cho bố cục tỷ lệ thuận nếu tôi thay đổi hướng. Bất kỳ trợ giúp sẽ được đánh giá cao.

Cảm ơn bạn.

+0

Bạn nên đọc về 'LinearLayout' &' weight'! –

+0

nếu bạn muốn bố cục 150dp hơn lý do tại sao bạn sử dụng khái niệm trọng lượng cho bố cục khác? –

+0

Xin chào, cảm ơn bạn rất nhiều về gợi ý. Tôi đã tìm thấy vấn đề ngay bây giờ. Tôi đã thiết lập chúng thành 'android: layout_height =" fill_parent "' nhưng quên đặt 'weight'. Xin lỗi, lỗi của người mới bắt đầu. – Wellsen

Trả lời

2

Bằng cách sử dụng layout_weight trên ba linearLayout và gán cho mỗi giá trị giống nhau, màn hình sẽ chia theo chiều dọc thành 3 bất kể kích thước màn hình. Ngoài ra, chuyển nhượng chiều cao 0dp: android:layout_height="0dp"

Các xml hoàn chỉnh sẽ là:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:baselineAligned="false" 
    android:orientation="vertical" > 

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

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:gravity="left" /> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:gravity="right" /> 
    </LinearLayout> 

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

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" /> 
    </LinearLayout> 

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

     <TextView 
      android:id="@+id/textView4" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:gravity="left" /> 

     <TextView 
      android:id="@+id/textView5" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:gravity="center" /> 

     <TextView 
      android:id="@+id/textView6" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:gravity="right" /> 
    </LinearLayout> 

    <SeekBar 
     android:id="@+id/seekBar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:max="255" /> 

</LinearLayout> 
+0

Xin chào, cảm ơn bạn rất nhiều, được đánh dấu là câu trả lời. – Wellsen

2

Bạn cũng có thể nhận được kích thước màn hình một cách thực dụng và thiết lập chiều cao/cân nặng dựa trên yêu cầu của bạn.

Display display = getWindowManager().getDefaultDisplay(); 
    DisplayMetrics outMetrics = new DisplayMetrics(); 
    display.getMetrics(outMetrics); 

    float density = getResources().getDisplayMetrics().density; 
    float dpHeight = outMetrics.heightPixels/density; 
    float dpWidth = outMetrics.widthPixels/density; 
4

Có vẻ như mọi người đã trả lời câu hỏi của bạn khi tôi đang thực hiện việc này. enter image description here Tuy nhiên, tôi đang đăng câu trả lời này trong trường hợp bạn vẫn cần trợ giúp. Hình ảnh này chứa các trọng số bạn cần gán cho bố cục của mình, được biểu thị bằng đồ thị. Xin lỗi một số thiếu liên kết, tôi đã thực hiện điều này một cách vội vàng.

7
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#555555"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:background="#555555"> 
     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="part1" 
      android:background="#008000"/> 
     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="part2" 
      android:background="#0000FF"/> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#ffffff"></LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:orientation="horizontal" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#000000"> 
     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="part1" 
      android:background="#008000"/> 
     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="part2" 
      android:background="#A9F114"/> 
     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="part3" 
      android:background="#B4155D"/> 
    </LinearLayout> 
</LinearLayout> 
Các vấn đề liên quan