【问题标题】:Inflating an image to be front and center of screen将图像膨胀到屏幕的前面和中心
【发布时间】:2013-04-20 19:58:43
【问题描述】:

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

【问题讨论】:

  • 你想在fullScreenroot上面添加这样的视图吗?
  • 目前我正在将新视图添加到fullscreen 内,因为我无法将它添加到顶级父元素。所以是的,我希望它高于所有视图,然后当我单击时将其取下。它应该是一个占据全屏的图像视图。
  • 我想你在一些活动中使用了这个布局。你把第一个sn-p代码放在哪里了?
  • onClickListener 中调用它,单击按钮后,我想放大与按钮关联的图片的全屏预览。
  • 你能发布你的活动代码吗?

标签: android layout-inflater


【解决方案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 并相应地设置其来源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    相关资源
    最近更新 更多