2016-01-30 42 views
1

在具有「下巴」的Android Wear設備上,屏幕尺寸仍以方塊形式報告,但您會看到此對象,其中包含有關從屏幕中裁剪的內容的信息。如何調整Android Wear上下巴的佈局?

我有一個配置屏幕,它已經正確地獲取這個值,很容易確認,因爲我的視圖渲染了屏幕形狀的輪廓。

public class ConfigView extends FrameLayout { 
    private Rect lastBounds = new Rect(); 
    private WindowInsets lastWindowInsets; 

    //... omitting initialisation 

    @Override 
    protected void onFinishInflate() { 
     super.onFinishInflate(); 
     requestApplyInsets(); 

     // omitting other stuff not relevant to this 

     GridViewPager pager = 
      (GridViewPager) findViewById(R.id.pager); 
     DotsPageIndicator dotsPageIndicator = 
      (DotsPageIndicator) findViewById(R.id.page_indicator); 
     dotsPageIndicator.setPager(pager); 
    } 

    @Override 
    public WindowInsets onApplyWindowInsets(WindowInsets insets) { 
     WindowInsets result = super.onApplyWindowInsets(insets); 

     lastWindowInsets = insets; 
     maybeInitialiseViews(); 

     return result; 
    } 

    @Override 
    protected void onLayout(boolean changed, int left, int top, 
          int right, int bottom) { 
     super.onLayout(changed, left, top, right, bottom); 

     lastBounds.set(left, top, right, bottom); 
     maybeInitialiseViews(); 
    } 

    private void maybeInitialiseViews() { 
     if (lastWindowInsets != null && !lastBounds.isEmpty()) { 
      GridViewPager pager = 
       (GridViewPager) findViewById(R.id.pager); 
      if (pager.getAdapter() == null) { 
       // TODO: Maybe there is a better callback I can rely on 
       // being done on every layout but not before applying 
       // insets, but a failure occurs later if we initialise 
       // the adapter before we have both bits of info. 
       pager.setAdapter(new ConfigGridPagerAdapter()); 
      } 
     } 
    } 
} 

我的問題是現在組件的定位。佈局XML:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.wearable.view.BoxInsetLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <example.ConfigView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <android.support.wearable.view.GridViewPager 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:keepScreenOn="true" 
      android:nestedScrollingEnabled="false"/> 

     <android.support.wearable.view.DotsPageIndicator 
      android:id="@+id/page_indicator" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal|bottom" 
      app:dotFadeWhenIdle="false"/> 

    </example.ConfigView> 

</android.support.wearable.view.BoxInsetLayout> 

雖然畫面由下巴截斷,系統給了ConfigView整個320×320的空間。這將列表的底部以及頁面指示器置於屏幕的底部。 我嘗試使用填充將它們手動移回,但填充似乎沒有效果,我不太確定原因。

我也試過使Config本身BoxInsetLayout和搞亂哪一方得到的插入,但這似乎並沒有改變任何視覺。

我試着把app:layout="bottom"放在ConfigView的XML中,這樣做可以使它適用於下巴布局,但是當沒有下巴時會添加一個不需要的邊框。

這應該如何工作?它讓我感到有點不知所措。當我在1920x1080顯示器上編程時,我不需要做任何特別的操作來裁剪1920x1920的盒子。

回答

4

我也在尋找墊我的佈局的底部因素在下巴的大小。我設法做到這一點,將所有內容都包裝在自定義的FrameLayout中,並在onApplyWindowInsets()中設置填充。

public class ChinLayout extends FrameLayout { 

    public ChinLayout(Context context) { 
     super(context); 
    } 

    public ChinLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public ChinLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public ChinLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    public WindowInsets onApplyWindowInsets(WindowInsets insets) { 
     int chin = insets.getSystemWindowInsetBottom(); 
     setPadding(0, 0, 0, chin); 
     return insets; 
    } 
} 

然後我在層次結構的根使用ChinLayout

<?xml version="1.0" encoding="utf-8"?> 
<ChinLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- View hierarchy to fit above the chin goes here. --> 

</ChinLayout> 
+0

setPadding確實出現做的工作。 – Trejkaz