【问题标题】:getLayoutParams() has not weight attribute (Android)getLayoutParams() 没有权重属性 (Android)
【发布时间】:2014-09-14 09:15:54
【问题描述】:

我试图在我的函数中获取android:layout_weight。这是我的 xml 代码:

    <LinearLayout
        android:id="@+id/myID"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="14"
        android:clickable="true"
        android:orientation="horizontal">
    ......
    </LinearLayout>

我的部分功能:

import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;

....
LinearLayout layout = (LinearLayout) findViewById(R.id.myID);

但是当我尝试调用layout.getLayoutParams() 时,没有weight 变量,只有宽度、高度、... 这是因为导入的包还是getLayoutParams()没有重量?

这个问题和这里一样,但他也解决不了:How do I get the `layout_weight` of a TextView?

【问题讨论】:

    标签: android


    【解决方案1】:

    视图中使用的布局参数的实际类型取决于它所在的容器类型。并非所有布局参数类型都有weight 字段。如果您的布局是另一个LinearLayout(或其派生类之一,例如TableLayoutTableRow)的一部分,那么您只需要将layout.getLayoutParams() 返回的值转换为正确的类型。例如:

    float weight = ((LinearLayout.LayoutParams) layout.getLayoutParams()).weight;
    

    您没有确切说明您的 layout 视图属于哪种布局类,所以我不能比这更具体。

    【讨论】:

    • 为我工作!非常感谢! @TedHopp 请,你认为你能帮我解决这个问题吗goo.gl/wO9G2N
    猜你喜欢
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    相关资源
    最近更新 更多