本文介绍了Java方法为Android:layout_gravity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法叫安卓:从Java方法layout_gravity 属性。我没有发现Android的文档的任何方法来做到这一点。这是布局我要实现的画面:

I would like to know if is there a way to call android:layout_gravity property from a Java method. I didn't found any method in Android documentation to do it. This is the picture of the layout I want to implement:

我知道通过XML做到这一点,如下:

I know to do it through XML, as following:

<FrameLayout
    xlmns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

      <Button
         android:layout_gravity="left|center_vertical"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="<" />


      <Button
         android:layout_gravity="right|center_vertical"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text=">" />
</FrameLayout>

但是,在我的情况,我需要通过Java code做到这一点,因为我会动态地实现另一个布局的意见。为了避免合并XML布局与Java code,我会$使用Java p $ PFER让所有的布局。

But in my situation, I need to do it through Java code, because I'll implement another layout views dynamically. To avoid merging XML layout with Java code, I would prefer make all layout using Java.

推荐答案

嘛,据我了解你在寻找这该是的FrameLayout其他布局看​​到合适的LayoutParams类。对于设置重力像在XML的使用是这样的:

Well as far as I understand you looking for this It is for FrameLayout for other layout see appropriate LayoutParams classes. For setting gravity like in your XML use something like:

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.LEFT | Gravity.CENTER_VERTICAL)

,然后按 addView 与新构造的的LayoutParams

这篇关于Java方法为Android:layout_gravity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:31