【问题标题】:add view dynamically in a fragment在片段中动态添加视图
【发布时间】:2013-03-06 09:44:11
【问题描述】:

我需要以议程样式显示一些信息,因此我使用片段来显示不同日期的信息。每天都有一些专栏来展示房间。

我创建了结构,但是当我尝试放入信息时遇到了一些问题

agenda_child_tv - textview 我放置信息的地方

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text_child"
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content"
    android:clickable="true"
    android:background="@drawable/back"
    android:padding="3sp"/>

agenda_child_ll - 列

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/agenda_aula_width"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<TextView
    android:id="@+id/agenda_aula_title" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

    <LinearLayout  
        android:id="@+id/agenda_aula_esp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

议程日

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
    android:layout_height="match_parent" >

<ScrollView 
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/horizontal_fragment"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="@dimen/agenda_tv_width"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="#BDBDBD" >

                <TextView 
                    android:layout_width="@dimen/agenda_tv_width"
                    android:layout_height="@dimen/agenda_tv_height"
                    android:text="9.00"
                    android:padding="@dimen/agenda_tv_padding"
                    android:textColor="@color/agenda_orario_color"
                    android:textSize="@dimen/agenda_tv_textsize" />

                <!-- A lot of textview for the hours -->

            </LinearLayout>

            <!-- Here i put, dinamically, the colum for the room in each day --> 

    </LinearLayout>

</ScrollView>

</HorizontalScrollView>

议程

下面的代码不是很动态,但我写了一个简化的版本来告诉你我的问题

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.agenda_day, container, false);

    LinearLayout dayLL = (LinearLayout) rootView.findViewById(R.id.horizontal_fragment);

    //Retriving LL agenda_child_ll
        LinearLayout aula = (LinearLayout) inflater.inflate(R.layout.agenda_child_ll, container, false);

        //Setting the classroom name
        TextView aulaName = (TextView) aula.findViewById(R.id.agenda_aula_title);
        aulaName.setText("room A");

        //Retriving LL agenda_aula_esp
        LinearLayout roomLL = (LinearLayout) aula.findViewById(R.id.agenda_aula_esp);

        //Retriving TV child
        TextView child = (TextView) inflater.inflate(R.layout.agenda_child_tv, container, false);
        child.setText("test");

        //Adding child to the room
        roomLL.addView(child);

        //adding room to the day
        dayLL.addView(aula);

        //Creating the column for the classroom "room B"

        LinearLayout aula2 = (LinearLayout) inflater.inflate(R.layout.agenda_child_ll, container, false);

        TextView aulaName2 = (TextView) aula2.findViewById(R.id.agenda_aula_title);
        aulaName2.setText("room B");

        dayLL.addView(aula2);
}

这就是结果。 textview 子项未添加到布局中。 有人能帮我吗? :)

image of the result

【问题讨论】:

  • 我觉得您应该尝试将 gridview 与适配器一起使用,而不是您正在做的事情。适配器旨在非常有效地处理动态数据
  • 谢谢。但我认为使用 gridwith 你只能垂直或水平滚动,不能同时滚动...我解决了这个问题,我更改了agenda_aula_title 中的 layout_height。

标签: android android-layout android-fragments android-viewpager


【解决方案1】:

我解决了这个问题,我更改了agenda_aula_title 中的layout_height。

我添加的文本视图显示在屏幕外。

【讨论】:

    【解决方案2】:

    我遇到了类似的问题并以相同的方式解决了它。我的应用程序有一个主要活动和两个片段。第一个片段是一个列表片段,它显示了一个公式列表。单击任何公式时,显示公式表达式的 FormulaExpressionFragment 将替换 FormulaTitlesFragment。我想为 FormulaExpressionFragment 中的公式表达式动态添加包含计算器的布局。我就是这样实现的。

    public class FormulaExpressionsFragment extends Fragment {
    
    private TextView mTextView;
    private TextView mFormulaTitle;
    LinearLayout formulaExpressionsLayout;
    View rootView;
    View formulaCalculator;
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            }
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            rootView = inflater.inflate(R.layout.fragment_formula_expressions, container, false);
            return rootView;
        }
    void displayFormulaExpression(int position){
        mTextView = (TextView) getView().findViewById(R.id.formula_expression_view);
        mFormulaTitle = (TextView) getView().findViewById(R.id.formula_title_view);
        mTextView.setText(getResources().getTextArray(R.array.formula_expressions)[position]);
        mFormulaTitle.setText(getResources().getTextArray(R.array.formula_titles)[position]);
        mFormulaTitle.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    }
    
    void displayFormulaCalculator(int position){
        formulaExpressionsLayout = (LinearLayout) rootView.findViewById(R.id.formula_expression_layout);
        LayoutInflater inflater = LayoutInflater.from(getActivity().getApplicationContext());
        formulaCalculator = inflater.inflate(R.layout.formula_1, formulaExpressionsLayout, false);
        formulaExpressionsLayout.addView(formulaCalculator);
    

    fragment_formula_expressions.xml

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/formula_expression_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/formula_title_view"
        android:text="Formula Title"
        android:padding="5dp" />
    <TextView
        android:id="@+id/formula_expression_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Formula Expression"
        android:padding="5dp" />
    

    公式_1.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/formula_calculator">
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/mud_density"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" × 0.052 × "/>
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/tvd"/>
    

    特别注意

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    

    它们应该设置为“wrap_content”,否则添加在它们下面的视图将不会显示在布局中,因为顶视图将占据所有显示高度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      相关资源
      最近更新 更多