2013-04-20 25 views
3

我想膨脹只有圖像的視圖才能在主佈局中心屏幕上方,到目前爲止,我只能添加充氣視圖作爲我的主要的第一個孩子佈局。我怎樣才能讓imageview充氣超過我的其他視圖並處於屏幕中央?使圖像膨脹成屏幕的正面和中心

LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     layout = inflater.inflate(R.layout.image_preview,(ViewGroup)findViewById(R.id.previewHolder)); 
     ((LinearLayout) (LinearLayout)findViewById(R.id.fullScreen)).addView(layout);//add inflated to main view 

主要佈局

<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:focusable="true" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
> 

<LinearLayout 
    android:id="@+id/fullScreen" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</LinearLayout> 

<TableLayout 
    android:id="@+id/root" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:focusableInTouchMode="true" 
    android:stretchColumns="*" > 
    ........................ 
    </TableLayout> 
    </LinearLayout> 

活動

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_testing_cam); 
      badge.setOnClickListener(btnListener); 
      } 
    private OnClickListener btnListener = new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      //SELECTS BUTTON ACTIONS 
     switch(v.getId()){ 

      case R.id.status: 
       finish(); 
       break; 
      default: 
       previewImages(v.getId()); 
       break; 
     } 
    } 
}; 
      //CARRY OUT IMAGE FUNCTIONS 
public void previewImages(int index){ 
    try{ 

     LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     layout = inflater.inflate(R.layout.image_preview,(ViewGroup)findViewById(R.id.previewHolder)); 
     ((LinearLayout) (LinearLayout)findViewById(R.id.fullScreen)).addView(layout); 



    ImageView iv = (ImageView)findViewById(R.id.previewImage); 
    BitmapDrawable drawable = (BitmapDrawable) ((ImageView) showCase.getChildAt(index)).getDrawable(); 
    Bitmap bitmap = drawable.getBitmap(); 
    iv.setImageBitmap(bitmap); 
    ((LinearLayout) (LinearLayout)findViewById(R.id.fullScreen)).bringToFront(); 
    }catch(Exception e){ Log.i("ERROR FLATE", e.toString()); } 
    return; 
} 

查看被充氣

<?xml version="1.0" encoding="utf-8"?> 

<ImageView 
    android:id="@+id/previewImage" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:adjustViewBounds="true" 
    android:scaleType="fitXY"/> 

+0

你想在'fullScreen'和'root'上面添加這樣一個視圖嗎? – 2013-04-20 20:59:21

+0

那麼目前我在'fullscreen'裏添加了新的視圖,因爲我無法將它添加到頂級父元素。所以是的,我希望它在所有的意見之上,然後當我點擊退出。假設它是一個全屏顯示的圖像視圖。 – kabuto178 2013-04-20 21:06:19

+0

我想你在一些活動中使用這種佈局。你在哪裏放置了第一部分代碼? – 2013-04-20 21:12:55

回答

1

如果你想添加上述全屏和根預覽你應該修改你的父母佈局。 LinearLayout垂直安排其子女,而在這種情況下,您需要使用FrameLayoutRelativeLayout。 事情是這樣的:

<RelativeLayout 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:focusable="true"> 

    <!-- your previous content --> 
    <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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin"> 

    <LinearLayout 
     android:id="@+id/fullScreen" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </LinearLayout> 

    <TableLayout 
     android:id="@+id/root" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:focusableInTouchMode="true" 
     android:stretchColumns="*" > 
     ........................ 
    </TableLayout> 

    </LinearLayout> 

    <!-- your preview --> 
    <ImageView 
    android:id="@+id/preview" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:visibility="gone"/> 

</RelativeLayout> 

所有您需要的點擊做的是的previewImageView可見性改變,以visible並相應地設置其源。

+0

謝謝你現在看看這個,看看它是否一切順利 – kabuto178 2013-04-20 21:53:44