2017-02-17 90 views
0

我想將代替「電視臺的」顯示的web視圖(如下面圖片中看到)。我想「電視」是始終在平板電腦上完全可見(它應該縮放電視作爲一個整體)。縮放的LinearLayout在Android的

我有電視的4幀。

頂部和底部框架:

  • 具有相同的高度
  • 具有相同的寬度

左右的框:

  • 唐「T具有相同的寬度
  • 具有相同的高度

電視必須保持寬度/高度比。

enter image description here

我曾嘗試以下佈局,但它只能在大屏幕上,它並沒有在較小的平板電腦屏幕尺寸比例都記錄下來。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:minWidth="25px" 
android:minHeight="25px"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:minWidth="25px" 
     android:minHeight="25px"> 
     <ImageView 
      android:src="@drawable/top" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/imageView1" /> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"    
      android:id="@+id/linearLayout1"> 
      <ImageView 
       android:src="@drawable/left" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/imageView3" /> 
      <android.webkit.WebView 
         android:layout_weight="1" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:id="@+id/webView1" /> 
      <ImageView 
       android:src="@drawable/right" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/imageView4" /> 
     </LinearLayout> 
     <ImageView 
      android:src="@drawable/bottom" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/imageView2" /> 
    </LinearLayout> 
</LinearLayout> 
+0

連看如何解決在此之前,我可以告訴你,我的第一種方法*不會*嵌套的線性佈局,這是非常不好的原因很多。 ,主要是表現,我的第一個方法是一個RelativeLayout,我的第二個方法看看ConstraintLayout是否可以幫助我支持它所支持的AspectRatio約束。 –

+0

@pskink我無法爲webview設置正確的頁邊距 – Noturab

+0

請記住,使用9patch很好,但會導致透支(因爲Web視圖將位於ImageView的頂部)。根據您的情況,這可能會*或不*成爲問題。 –

回答

0

好的,所以我重新創建了一個平面層次結構的佈局。以此爲起點。唯一沒有建模的部分是AspectRatio,你必須處理這個問題。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
    > 
    <ImageView 
     android:id="@+id/imageview_top" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_alignParentTop="true" 
     android:background="@android:color/holo_blue_bright" 
     /> 
    <ImageView 
     android:id="@+id/imageview_start" 
     android:layout_width="50dp" 
     android:layout_height="200dp" 
     android:layout_alignParentStart="true" 
     android:layout_below="@id/imageview_top" 
     android:background="@android:color/holo_blue_light" 
     /> 
    <ImageView 
     android:id="@+id/imageview_end" 
     android:layout_width="50dp" 
     android:layout_height="200dp" 
     android:layout_alignParentEnd="true" 
     android:layout_below="@id/imageview_top" 
     android:background="@android:color/holo_blue_light" 
     /> 
    <ImageView 
     android:id="@+id/imageview_bottom" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_below="@id/imageview_start" 
     android:background="@android:color/holo_blue_bright" 
     /> 
    <android.webkit.WebView 
     android:id="@+id/webview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/imageview_bottom" 
     android:layout_below="@id/imageview_top" 
     android:layout_toEndOf="@id/imageview_start" 
     android:layout_toStartOf="@id/imageview_end" 
     android:background="@android:color/darker_gray" 
     /> 
</RelativeLayout> 

而ASPECTRATIO?

ConstraintLayout或許可以幫助你在那裏(比RelativeLayout的更多)。或者你必須包裹的WebView在自定義ViewGroup重寫onLayout/onMeasure和做魔術給你。

硬編碼圖像高度/隨機移民的寬度,但這樣你就需要指定一個大小或圖像視圖會崩潰到0/0,你可以換,如果你有圖片(我只用背景色內容。

它看起來像這樣:

Preview

+0

我不認爲這個解決方案有任何「解決方法」,沒有自定義代碼(如果ImageViews具有正確的高寬比,那麼WebView就是這樣,它可能不需要老實說話,那麼WebView也是如此。它平坦,快速,易於維護並進行測試編寫一個自定義的View組可能會產生進一步的影響和意想不到的副作用,我已經完成了,而且我看到它發生了。在沒有想到的情況下沒有任何問題。 –