相关疑难解决方法(0)

Android getMeasuredHeight返回错误的值!

我正在尝试确定一些UI元素的实际像素尺寸!

这些元素从.xml文件中膨胀,并使用dip宽度和高度进行初始化,以便GUI最终支持多个屏幕大小和dpi(如android规范所推荐).

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

<ImageView
    android:id="@+id/TlFrame" 
    android:layout_width="110dip" 
    android:layout_height="90dip"
    android:src="@drawable/timeline_nodrawing"
    android:layout_margin="0dip"
    android:padding="0dip"/></LinearLayout>
Run Code Online (Sandbox Code Playgroud)

此前的xml表示一帧.但是我在这里描述的水平布局中动态添加了许多:

<HorizontalScrollView 
        android:id="@+id/TlScroller"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_margin="0dip"
        android:padding="0dip"
        android:scrollbars="none"
        android:fillViewport="false"
        android:scrollbarFadeDuration="0"
        android:scrollbarDefaultDelayBeforeFade="0"
        android:fadingEdgeLength="0dip"
        android:scaleType="centerInside">

        <!-- HorizontalScrollView can only host one direct child -->
        <LinearLayout 
            android:id="@+id/TimelineContent" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_margin="0dip"
            android:padding="0dip"
            android:scaleType="centerInside"/>

    </HorizontalScrollView > 
Run Code Online (Sandbox Code Playgroud)

定义为在我的java代码中添加一个框架的方法:

private void addNewFrame()
{       
    LayoutInflater inflater     = (LayoutInflater) _parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewGroup root      = (ViewGroup) inflater.inflate(R.layout.tl_frame, null);
    TextView frameNumber = (TextView) root.findViewById(R.id.FrameNumber);
    Integer val = new Integer(_nFramesDisplayed+1); //+1 …
Run Code Online (Sandbox Code Playgroud)

android android-widget android-ui android-layout android-xml

37
推荐指数
4
解决办法
4万
查看次数

如何在"onBindViewHolder"中获取recyclerview项目的高度

RecyclerView项目的高度不固定,我需要为每个项目设置背景图像,所以我想获得recyclerview项目的高度来调整Image的大小,但 itemView.getHeight()总是返回0 in onBindViewHolder.

我试着搜索很多问题或文章,但我仍然无法获得良好的解决方案.

android kotlin android-recyclerview

19
推荐指数
3
解决办法
3万
查看次数

即使在从GONE变为VISIBLE之前,也可以准确地确定视图的测量高度

目前,我的布局看起来像这样.它包含.

  • 标题文本视图和价格文本视图,始终可见.
  • 说明文本视图,可以显示或消失,具体取决于展开或折叠.

折叠(在应用启动期间)

在此输入图像描述

扩展(当用户点击它时)

在此输入图像描述

我希望周围有一些不错的动画.所以,我提到了/sf/answers/936685991/

其中一个关键因素是,即使在可见之前,也要知道描述文本视图的确切高度.

但是,我意识到了一些类型的代码.他们不准确

// v is description text view.
v.measure(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
final int targtetHeight = v.getMeasuredHeight();
Run Code Online (Sandbox Code Playgroud)
// v is description text view.
v.measure(MeasureSpec.makeMeasureSpec(LayoutParams.MATCH_PARENT, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, MeasureSpec.EXACTLY));
final int targtetHeight = v.getMeasuredHeight();
Run Code Online (Sandbox Code Playgroud)

这将返回值32.(正确的测量高度假设为92).这是第一行文本的高度.这最终我的动画结束了

在此输入图像描述

我是否知道,确定视图的测量高度的正确方法是什么,甚至在它从GONE变为VISIBLE之前?

我的布局代码如下:

        <LinearLayout
            android:clickable="true"
            android:id="@+id/chart_linear_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/dummy"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="0dp"
                    android:width="0dp"
                    android:layout_weight="0.6"
                    android:layout_height="match_parent"
                    android:gravity="left"
                    android:textSize="20sp" 
                    android:textColor="#ff000000"
                    android:text="Summary chart" />
                <TextView
                    android:id="@+id/chart_price_text_view"
                    android:layout_width="0dp"
                    android:width="0dp"
                    android:layout_weight="0.4"
                    android:layout_height="match_parent"
                    android:gravity="right"
                    android:textSize="20sp" 
                    android:textColor="#ffF76D3C"
                    android:text="$2.99" /> …
Run Code Online (Sandbox Code Playgroud)

android

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

如何获取android应用程序窗口的大小而不是物理屏幕大小?

在Android中,如何获得应用程序窗口的大小?我不是在寻找物理屏幕大小,而是在屏幕上查看应用程序窗口的实际大小.例如,一般在你拿走通知栏,软触按钮等后剩下的东西.

android dimensions

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

TranslateAnimation - 动画仅在第二次之后起作用

我使用了一些我在滑动面板上找到的代码,基本上它可以工作但是有一个小问题.

第一次打开面板时动画不起作用.

这是动画的代码:

    TranslateAnimation anim = null;


    m_isOpen = !m_isOpen;

    if (m_isOpen) {
        setVisibility(View.VISIBLE);
        anim = new TranslateAnimation(0.0f, 0.0f, getHeight(), 0.0f);
    } else {
        anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, getHeight());
        anim.setAnimationListener(new Animation.AnimationListener() {
              public void onAnimationEnd(Animation animation) {
                   setVisibility(View.GONE);
              }

              public void onAnimationRepeat(Animation animation) {
                   // not needed
              }

              public void onAnimationStart(Animation animation) {
                   // not needed
              }
        });

    }

    anim.setDuration(300);
    anim.setInterpolator(new AccelerateInterpolator(1.0f));
    startAnimation(anim);
Run Code Online (Sandbox Code Playgroud)

为什么在第一次打开面板时没有动画,但其他所有动画都有?

animation android

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