2010-12-16 5 views
7

가로 세로 비율을 유지하기 위해 TabWidget 배경 이미지의 크기를 조정해야합니다.
TabWidget과 함께 TabHost를 사용하고 있습니다. 그런 다음 setBackgroundDrawable을 사용하여 이미지를 설정합니다.TabHost/TabWidget - 스케일 배경 이미지?

여기에 가까운 대답을 찾았습니다 - Background in tab widget ignore scaling. 그러나 새로운 Drawable 코드를 추가 할 위치가 확실하지 않습니다. (HelloTabWidget 예제로 작업하면 어떤 모듈도 RelativeLayout을 사용하지 않으며 "tabcontent"레이아웃이 보이지 않습니다.)

이 스레드는 - Android: Scale a Drawable or background image?입니다. 그것에 따르면, 이미지를 미리 조절해야만하는 것처럼 들리 겠지만, 이미지를 확장 할 수있는 전체 목적에 어긋납니다.

또 다른 스레드가 Drawable 클래스를 서브 클래 싱하여 크기를 조정하지 않거나 제대로 확장 할 수있는 스레드를 발견했습니다. 지금은 찾을 수 없지만 mTab.setScaleType (centerInside)과 같은 간단한 작업을 수행 할 수 있어야 할 때가 많습니다.

여기 내 코드입니다 :

main.xml에 :

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/main_background"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"/>   
    <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0"/> 
    </LinearLayout> 
</TabHost> 

주요 활동 :

 tabHost.setOnTabChangedListener(new OnTabChangeListener() { 
      TabHost changedTabHost = getTabHost(); 
      TabWidget changedTabWidget = getTabWidget(); 
      View changedView = changedTabHost.getTabWidget().getChildAt(0); 

      public void onTabChanged(String tabId) { 
       int selectedTab = changedTabHost.getCurrentTab(); 
       TabWidget tw = getTabWidget(); 

       if(selectedTab == 0) { 
        //setTitle("Missions Timeline"); 
        View tempView = tabHost.getTabWidget().getChildAt(0); 
        tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_timeline_on)); 
        tempView = tabHost.getTabWidget().getChildAt(1); 
        tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_map_off)); 
        tempView = tabHost.getTabWidget().getChildAt(2); 
        tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_search_off)); 
        tempView = tabHost.getTabWidget().getChildAt(3); 
        tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_news_off)); 
        tempView = tabHost.getTabWidget().getChildAt(4); 
        tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_license_off)); 
          //ImageView iv = (ImageView)tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon); 
          //iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_timeline_on)); 
          //iv = (ImageView)tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon); 
          //iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_off)); 
        } else if (selectedTab == 1) { 
         //setTitle("Spinoffs Around You"); 
         View tempView = tabHost.getTabWidget().getChildAt(0); 
         tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_timeline_off)); 
         tempView = tabHost.getTabWidget().getChildAt(1); 
         tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_map_on)); 
         tempView = tabHost.getTabWidget().getChildAt(2); 
         tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_search_off)); 
         tempView = tabHost.getTabWidget().getChildAt(3); 
         tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_news_off)); 
         tempView = tabHost.getTabWidget().getChildAt(4); 
         tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_license_off)); 
} 

나는 또한 이미지를 9patch했지만, 그들은 너무 작은 것을 바람.

그래서이 문제를 해결하는 가장 좋은 방법은 무엇입니까?

답변

0

배경 이미지 스케일링을 수행하는 기본 방법을 찾는데 어려움을 겪고있는 이유는 일반적으로 좋은 습관으로 보이지 않는다는 것입니다. 이미지의 크기 조정은 유물과 밴딩을 유발하여 사용자의 UI가 더러운 것처럼 보일 수 있습니다. 이것에 대한 예외는 Imageview 내에 있지만,이 클래스가 제공하는 확장 지원은 사진이나 웹 콘텐츠 (예 : 앱과 함께 제공되지 않은 이미지)를 지원하기위한 것입니다.

사용자의 UI 레이아웃은 리소스 기반 배경 이미지가 장치의 밀도/화면 크기에 맞는 올바른 해상도로 미리 조정되어야합니다. 즉, Supporting Multiple Screens dev 가이드에 설명 된 리소스 폴더 명명 규칙을 사용하여 여러 화면 밀도와 크기를 처리 할 수 ​​있도록 각 드로어 블의 여러 버전을 제공해야합니다.

그런 다음 포함 된보기의 배경 이미지를 조정하지 않고도 장치 간의 화면 크기 차이를 처리 할 수 ​​있도록 UI를 배치해야합니다. 분명히 제안 된 UI가 어떤 모양인지 모르기 때문에 구체적인 제안을하기가 어렵지만 일반적으로 간단한 배경 블록 색상 또는 ShapeDrawable을 사용하여 뷰 사이의 공간을 동적으로 채 웁니다.

위의 설교에도 불구하고 배경 이미지의 크기를 조정하려는 경우 ScaleDrawable을 사용하여 기존 드로어 블을 감싸 주시겠습니까? 둘 다보기 및 배경 이미지 묘화의 높이와 너비를 알고있는 경우

, 당신은 같은 것을 수행 할 수 있습니다

Drawable backgroundDrawable = getResources().getDrawable(R.drawable.tab_license_off); 
tempView.setBackgroundDrawable(
    new ScaleDrawable(
     backgroundDrawable, 
     Gravity.CENTER, 
     tempView.getWidth()/backgroundDrawable.getIntrinsicWidth(), 
     tempView.getHeight()/backgroundDrawable.getIntrinsicHeight());