【问题标题】:Image alligned not set in Center using FrameLayout使用 FrameLayout 未在中心设置对齐的图像
【发布时间】:2015-08-06 09:41:19
【问题描述】:

我正在尝试从这个tutorial 中学习FrameLayout

http://javatechig.com/android/android-framelayout-example

它在FrameLayout 中添加一个imageView 并尝试将图像置于中心

    android:scaleType="centerCrop"

但它不工作
图片显示是这样的

这里是activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_launcher" />

        </FrameLayout>

这是类文件

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

是不是有些新属性需要替换??
图像在相对布局中对齐良好,但在框架布局中不对齐。有什么建议吗?

【问题讨论】:

  • 在你的图像视图中使用 layout_gravity="center"
  • scaleType 仅用于确定如何在 imageView 本身中缩放位图。

标签: android eclipse android-framelayout


【解决方案1】:

@user3734952:仅供参考

android:layout_gravity="center"

是一个布局参数。并非所有视图组都支持此布局参数。请参阅文档以了解特定视图组支持哪些布局参数。线性布局确实支持android:layout_gravity 布局参数。将对象放置在其容器的中心,在垂直和水平轴上,不改变其大小。

如需更好的方法,请添加android:adjustViewBounds="true"。如果您希望 ImageView 调整其边界以保持其可绘制对象的纵横比,请将其设置为 true。

【讨论】:

    【解决方案2】:

    将布局重力设置为 imageView。

     android:layout_gravity="center"
    

    取决于您的要求,您可以提供“layout_gravity”,例如:

    center_vertical or center_horizontal. 
    

    【讨论】:

      【解决方案3】:

      scaleType 不是指图像在其父布局中的放置方式,而是如何将图像放置在其自己的边界布局中。见ScaleType link

      如果您想调整左、下或居中对齐方式,可以在 ImageView 中使用 android:layout_gravity 或在其父布局中设置 android:gravity

      【讨论】:

        【解决方案4】:

        试试这个

        <?xml version="1.0" encoding="utf-8"?>
        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_launcher" />
        

        【讨论】:

          猜你喜欢
          • 2012-02-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-28
          • 1970-01-01
          相关资源
          最近更新 更多