相关疑难解决方法(0)

View的getWidth()和getHeight()返回0

我动态地在我的android项目中创建所有元素.我试图获得按钮的宽度和高度,以便我可以旋转该按钮.我只是想学习如何使用android语言.但是,它返回0.

我做了一些研究,我发现它需要在onCreate()方法之外的某个地方完成.如果有人能给我一个如何做的例子,那就太好了.

这是我目前的代码:

package com.animation;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;

public class AnimateScreen extends Activity {


//Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout ll = new LinearLayout(this);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(30, 20, 30, 0);

    Button bt = new Button(this);
    bt.setText(String.valueOf(bt.getWidth()));

    RotateAnimation ra = new RotateAnimation(0,360,bt.getWidth() / 2,bt.getHeight() / 2);
    ra.setDuration(3000L);
    ra.setRepeatMode(Animation.RESTART);
    ra.setRepeatCount(Animation.INFINITE);
    ra.setInterpolator(new LinearInterpolator());

    bt.startAnimation(ra);

    ll.addView(bt,layoutParams);

    setContentView(ll);
} …
Run Code Online (Sandbox Code Playgroud)

java getter android android-layout

487
推荐指数
12
解决办法
30万
查看次数

如何获得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:以编程方式获取viewHeight

我试图获得一个视图的高度,它一直返回0.

这是我尝试过的:

View hourView = View.inflate(mContext, R.layout.calendar_hour_item,null);
hourViewHeight = hourView.findViewById(R.id.mainLayout).getHeight();
Run Code Online (Sandbox Code Playgroud)

我也试过hourViewHeight = hourView.getHeight();但没有快乐.

我在这里的initialize()方法中调用这两行:

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

        Intent intent = getIntent();

        mDate = new DateTime(intent.getStringExtra("date"));
        circleIcon = (ImageView) findViewById(R.id.circle);

        mHoursLayout = (RelativeLayout) findViewById(R.id.dayLayout);

        TopBarUtil topBar = new TopBarUtil(getApplicationContext());

        circleIcon.setOnClickListener(topBar.onActionClickListener());

        mContext = getApplicationContext();



        initialize();
    }
Run Code Online (Sandbox Code Playgroud)

这是我的xml布局:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:id="@+id/mainLayout" android:background="@layout/border">

    <TextView android:text="12am" android:id="@+id/hourTextView"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我basicllay想要获得上面的xml布局的高度,但它总是返回0.

编辑:我也尝试过这样做:

@Override
    protected void onStart() {
        // TODO Auto-generated method stub …
Run Code Online (Sandbox Code Playgroud)

android

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

标签 统计

android ×3

android-layout ×1

getter ×1

height ×1

image ×1

java ×1

width ×1