【问题标题】:Android - add subview with layout params at specific index (in code)Android - 在特定索引处添加带有布局参数的子视图(在代码中)
【发布时间】:2012-02-14 07:29:07
【问题描述】:

我可能遗漏了一些东西,但我找不到将子视图添加到具有特定布局参数的指定索引处的视图的方法。有 addView(View v, LayoutParams lp)addView(View v, int index) - 但我找不到同时指定两者的方法。

我错过了什么?

【问题讨论】:

    标签: android android-view layoutparams


    【解决方案1】:

    为什么,有一种方法可以同时使用:addView(View child, int index, ViewGroup.LayoutParams params):Here

    【讨论】:

    • 嗯,真的。为什么我的日食抱怨?
    【解决方案2】:

    这是一个例子。我用它在任何视图上显示进度指示器。

    final ViewGroup rootFrameLayout = (ViewGroup) activity.getWindow().peekDecorView();
    LayoutInflater li = LayoutInflater.from(activity);
    progressView = li.inflate(R.layout.progressindicator, null);
    rootFrameLayout.addView(progressView);
    

    这是一个示例布局文件(id 是 R.layout.progressindicator)

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" />
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="3dp"
        android:layout_toRightOf="@+id/progressBar1"
        android:maxLines="3"
        android:lines="3"
        android:background="#88000000"
        android:textColor="#ffffff"
        android:text="@string/waitingForServer" />
    

    以防万一您还想动态更改布局,您可以调用布局中元素的方法。像这样:

    TextView textView = (TextView) progressView.findViewById(R.id.textView1);
    textView.setMaxLines(lines);
    textView.setLines(lines);
    textView.setVisibility(View.VISIBLE);
    

    【讨论】:

    • 我不确定这如何回答这个问题。你在哪里指定布局参数?
    • @AleksG 我添加了布局文件并解释了它是如何使用的。
    • 我仍然不明白这一切是如何回答这个问题的。我没有任何布局文件 - 我在代码中动态创建视图。我需要将视图添加到父视图 - 在代码中动态 - 同时指定布局参数。您的回答不会尝试执行任何操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    相关资源
    最近更新 更多