如何获取视图相对于其父视图的位置,包括内边距?

huangapple go评论76阅读模式
英文:

How to get the position of a view relative to its parent including padding?

问题

我有一个FrameLayout,其中包含一个LinearLayout子视图。我通过将其底部内边距设置为100来更新LinearLayout的位置。但是当我收到OnLayoutChange回调时,bottomoldBottom的值是相同的。

我尝试过offsetDescendantRectToMyCoordsgetLocationInWindowgetGlobalVisibleRect,但似乎都没有考虑到内边距。

以下是我的布局文件:
activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/frameLayout">

</FrameLayout>

banner_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/banner_layout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="bottom">

    <LinearLayout
        android:id="@+id/banner_container"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/banner_bgcolor">

        <TextView
            android:id="@+id/banner_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-light"
            android:padding="2dp"
            android:textSize="15sp"
            android:textColor="#FF323232"
            android:textColorLink="#FF828282"
            android:textIsSelectable="false"
            android:textStyle="normal"
            android:layout_weight="1"
            android:layout_gravity="left"
            android:text="Banner text here"/>

        <TextView
            android:id="@+id/banner_logotext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingBottom="2dp"
            android:paddingTop="2dp"
            android:paddingRight="10dp"
            android:textSize="15sp"
            android:textColor="#FF828282"
            android:textColorLink="#FF828282"
            android:textIsSelectable="false"
            android:textStyle="normal"
            android:layout_gravity="right"
            android:text="Logo"/>
    </LinearLayout>

</LinearLayout>

还有我的主Activity:

class MainActivity : AppCompatActivity() {
    var bottom = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val frameLayout = findViewById<FrameLayout>(R.id.frameLayout)
        val bannerView = frameLayout.addBannerView()
        frameLayout.setOnClickListener {
            bannerView.post {
                bottom += 32
                bannerView.setPadding(0, 0, 0, bottom)
            }
        }
    }

    private fun FrameLayout.addBannerView(): View {
        val inflater = LayoutInflater.from(context)
        inflater.inflate(R.layout.banner_layout, this, true)
        val bannerView = findViewById<View>(R.id.banner_layout)
        bannerView.addOnLayoutChangeListener { view, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
            val msg = "Difference bottom: ${bottom - oldBottom}\n" +
                "Position top left: (${bannerView.left}, ${bannerView.top})"
            Log.e("MainActivity", msg)
            Toast.makeText(context, msg, Toast.LENGTH_LONG).show()
        }
        return bannerView
    }
}

基本上,上面的代码在onCreate中将bannerView添加到frameLayout,并在点击时将32dp添加到bannerView的底部内边距。然而,无论我如何滑动视图,得到的输出始终是这样的:

Difference bottom: 0
Position top left: (0, 0)
英文:

I have a FrameLayout with a LinearLayout child. I am updating the position of the LinearLayout by setting its bottom padding to 100. However when I get the OnLayoutChange callback, the bottom and oldBottom are identical.

I have tried offsetDescendantRectToMyCoords, getLocationInWindow, and getGlobalVisibleRect but it doesn't seem like any of these take into account padding.

Here is are my layout files:
activity_main.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;
    android:id=&quot;@+id/frameLayout&quot;&gt;

&lt;/FrameLayout&gt;

banner_layout.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:id=&quot;@+id/banner_layout&quot;
    android:orientation=&quot;vertical&quot;
    android:layout_width=&quot;fill_parent&quot;
    android:layout_height=&quot;fill_parent&quot;
    android:gravity=&quot;bottom&quot;&gt;

    &lt;LinearLayout
        android:id=&quot;@+id/banner_container&quot;
        android:orientation=&quot;horizontal&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:background=&quot;@drawable/banner_bgcolor&quot;&gt;

        &lt;TextView
            android:id=&quot;@+id/banner_text&quot;
            android:layout_width=&quot;0dp&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:fontFamily=&quot;sans-serif-light&quot;
            android:padding=&quot;2dp&quot;
            android:textSize=&quot;15sp&quot;
            android:textColor=&quot;#FF323232&quot;
            android:textColorLink=&quot;#FF828282&quot;
            android:textIsSelectable=&quot;false&quot;
            android:textStyle=&quot;normal&quot;
            android:layout_weight=&quot;1&quot;
            android:layout_gravity=&quot;left&quot;
            android:text=&quot;Banner text here&quot;/&gt;

        &lt;TextView
            android:id=&quot;@+id/banner_logotext&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:paddingLeft=&quot;10dp&quot;
            android:paddingBottom=&quot;2dp&quot;
            android:paddingTop=&quot;2dp&quot;
            android:paddingRight=&quot;10dp&quot;
            android:textSize=&quot;15sp&quot;
            android:textColor=&quot;#FF828282&quot;
            android:textColorLink=&quot;#FF828282&quot;
            android:textIsSelectable=&quot;false&quot;
            android:textStyle=&quot;normal&quot;
            android:layout_gravity=&quot;right&quot;
            android:text=&quot;Logo&quot;/&gt;
    &lt;/LinearLayout&gt;

&lt;/LinearLayout&gt;

And my main activity:

class MainActivity : AppCompatActivity() {
    var bottom = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val frameLayout = findViewById&lt;FrameLayout&gt;(R.id.frameLayout)
        val bannerView = frameLayout.addBannerView()
        frameLayout.setOnClickListener {
            bannerView.post {
                bottom += 32
                bannerView.setPadding(0,0,0, bottom)
            }
        }
    }

    private fun FrameLayout.addBannerView(): View {
        val inflater = LayoutInflater.from(context)
        inflater.inflate(R.layout.banner_layout, this, true)
        val bannerView = findViewById&lt;View&gt;(R.id.banner_layout)
        bannerView.addOnLayoutChangeListener { view, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom -&gt;
            val msg = &quot;Difference bottom: ${bottom - oldBottom}\n&quot; +
                &quot;Position top left: (${bannerView.left}, ${bannerView.top})&quot;
            Log.e(&quot;MainActivity&quot;, msg)
            Toast.makeText(context, msg, Toast.LENGTH_LONG).show()
        }
        return bannerView
    }
}

Basically, the above code adds the bannerView to the frameLayout in onCreate, and adds an onClick that adds 32dp to the bottom padding of the bannerView. The output I get is always like this, even though I can see the view moving up the screen.

Difference bottom: 0
Position top left: (0, 0)

答案1

得分: 1

不使用代码,很难理解发生了什么。请尝试分享代码。

但是,听起来你正在尝试通过调整内部子视图的填充来更新 FrameLayout 中的位置,但在 OnLayoutChange 回调中无法获得更新后的位置信息。

可能的一个问题是,OnLayoutChange 回调可能会在填充更改生效之前被调用。这可能是因为填充更改没有立即生效,而是在下一次布局过程中排队等待应用。

为了确保填充更改在调用 OnLayoutChange 回调之前生效,你可以尝试使用 View.post() 方法来安排填充更改在当前布局过程完成后应用。例如,你可以像这样做:

myLinearLayout.post(new Runnable() {
    @Override
    public void run() {
        myLinearLayout.setPadding(0, 0, 0, 100);
    }
});

这将确保填充更改在下一次布局过程中应用,这应该确保 OnLayoutChange 回调可以访问到更新后的位置信息。

英文:

Without code, it's hard to understand what is happening. Please, try to share the code.

But, it sounds like you are trying to update the position of a child view within a FrameLayout by adjusting its padding, but you are having trouble getting the updated position information from the OnLayoutChange callback.

One possible issue is that the OnLayoutChange callback may be called before the padding changes take effect. This could be because the padding changes are not being applied immediately, but rather are being queued up to be applied during the next layout pass.

To ensure that the padding changes are applied before the OnLayoutChange callback is called, you can try using the View.post() method to schedule the padding changes to be applied after the current layout pass has completed. For example, you could do something like this:

myLinearLayout.post(new Runnable() {
    @Override
    public void run() {
        myLinearLayout.setPadding(0, 0, 0, 100);
    }
});

This will ensure that the padding changes are applied during the next layout pass, which should ensure that the OnLayoutChange callback has access to the updated position information.

huangapple
  • 本文由 发表于 2023年2月24日 01:55:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定