将图像充气为屏幕的正面和中心

时间:2013-04-20 19:58:43

标签: android layout-inflater

我想在一个只有一个图像的视图上面放置我的主要布局中心屏幕,到目前为止,我只能添加一个膨胀的视图作为我主要布局的第一个孩子。如何将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"/>

1 个答案:

答案 0 :(得分:1)

如果要在fullScreen和root上面添加预览,则应修改父布局。 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>

点击即可将preview ImageView的可见度更改为visible并相应地设置其来源。