onResume中ImageView的宽度仍为0?

时间:2014-07-26 17:57:39

标签: android android-imageview

我有一个TextView和ImageView,它应该超出可用的宽度:

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:text="TextView" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

我试图获得imgageview的宽度,以填充最佳尺寸的图像。但它是0,也在onResume。

活动和片段:

package com.example.getmap;


import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);

            TextView text = (TextView) rootView.findViewById(R.id.textView1);
            text.setText("This is just some text\nin two lines");

            ImageView map = (ImageView) rootView.findViewById(R.id.imageView1);
            int mMeasuredWidth = map.getMeasuredWidth();
            int mWidth = map.getWidth();

            Toast.makeText(getActivity(), "width: " + mWidth + "\nmMeasuredWidth: " + mMeasuredWidth , Toast.LENGTH_SHORT).show();

            return rootView;
        }
    }
}

我尝试使用OnGlobalLayoutListener,但这不起作用,因为我有几个这个片段的实例。

有什么建议吗?

解决方案:

最后我通过以下方式解决了问题:

text.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);

0 个答案:

没有答案