相关疑难解决方法(0)

如何获得android.widget.ImageView的宽度和高度?

????????????????????????????????????????????????   ^
? ImageView    ????????????????                ?   |
?              ?              ?                ?   |
?              ? Actual image ?                ?   |
?              ?              ?                ?   |60px height of ImageView
?              ?              ?                ?   |
?              ?              ?                ?   |
?              ????????????????                ?   |
????????????????????????????????????????????????   
<------------------------------------------------>
                   90px width of ImageView
Run Code Online (Sandbox Code Playgroud)

我有一个默认高度和宽度的图像视图,图像存储在数据库中,我想根据Imageview高度宽度缩放图像.因为我不希望它给出默认值,因为当我改变它的高度和宽度时我也必须在代码中更改它.

我试图获得ImageView的高度和宽度,但在两种情况下都返回0.

int height = ((ImageView) v.findViewById(R.id.img_ItemView)).getHeight();
Run Code Online (Sandbox Code Playgroud)

即使它有默认的高度和宽度,这也会返回0

height android image width

270
推荐指数
7
解决办法
14万
查看次数

Android以编程方式创建视图的宽度

我以编程方式创建TextView,如下所示:

                TextView mTextView = new TextView(getApplicationContext());

                final LayoutParams params = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                ((MarginLayoutParams) params).setMargins(8, 8, 0, 0);

                mTextView.setText(mText);
                mTextView.setLayoutParams(params);
                mTextView.setPadding(2, 2, 2, 2);
                mTextView.setBackground(getResources().getDrawable(R.drawable.note_corps));

                tagMap.addView(mTextView);

                textViewsWidth = mTextView.getWidth();
Run Code Online (Sandbox Code Playgroud)

但是mTextView.getWidth()始终返回0

如果我尝试:

mTextView.getLayoutParams().width
Run Code Online (Sandbox Code Playgroud)

它返回LayoutParams类中的LayoutParams对应值(-1或-2)

如何获得视图的宽度?

编辑我需要在这里这样做:

            @Override
        public void onPostExecute(Hashtable<String, Integer> hash){
            final ScrollView tagMapScroll = (ScrollView) findViewById(R.id.tagMapScroll);
            final LinearLayout tagMap = (LinearLayout) findViewById(R.id.tagMap);
            final ArrayList<Integer> arr = new ArrayList<Integer>(hash.values());
            final Enumeration<String> e = hash.keys();
            int index = 0;
            int textViewsWidth = 0;

            while(e.hasMoreElements()){
                final TextView tV = new TextView(getApplicationContext()); …
Run Code Online (Sandbox Code Playgroud)

android textview

20
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×2

height ×1

image ×1

textview ×1

width ×1