将视图添加到Android中未显示的片段

将视图添加到Android中未显示的片段,android,android-fragments,android-linearlayout,android-xml,Android,Android Fragments,Android Linearlayout,Android Xml,我有一个片段,我试图在其中添加一个类“CategoryOption”,它扩展了LinearLayout。但是,当我在我的onCreateView()中添加视图时,它没有显示任何内容 以下是我的片段代码: public class ContentFragment extends Fragment { LinearLayout navBarLayout; @Override public View onCreateView(LayoutInflater inflater, ViewGroup c

我有一个片段,我试图在其中添加一个类“CategoryOption”,它扩展了
LinearLayout
。但是,当我在我的
onCreateView()
中添加视图时,它没有显示任何内容

以下是我的片段代码:

public class ContentFragment extends Fragment {

LinearLayout navBarLayout;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.content_fragment, container, false);

    navBarLayout = (LinearLayout) view.findViewById(R.id.navbar_layout);
    navBarLayout.addView(new CategoryOption(getActivity(), R.drawable.ic_action_rss, "RSS Feed"));

    return view;    
}
}
以下是我的内容片段xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<HorizontalScrollView
    android:id="@+id/horizontalScrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/navbar_layout"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray"
        android:orientation="horizontal" >

    </LinearLayout>
</HorizontalScrollView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

</LinearLayout>

}

由于您的
类别选项
视图正在扩展线性布局,请尝试删除
选项布局
字段,并将代码更改为类似以下内容:

public CategoryOption(Context context, int iconId, String optionName) {
    super(context);

    this.iconId = iconId;
    this.optionName = optionName;

    tvOption = new TextView(context);
    ivIcon = new ImageView(context);

    setupLayout();
    setupTextView();
    setupImageView();

    addView(ivIcon);
    addView(tvOption);
}

private void setupLayout() {
    // TODO Auto-generated method stub
    setLayoutParams(new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    setOrientation(LinearLayout.VERTICAL);
    setGravity(Gravity.CENTER);
    setPadding(4, 4, 4, 4);
    setBackgroundColor(Color.parseColor("#DEDEDE"));
}
您所做的似乎是创建对另一个LinearLayout(似乎没有必要)的引用,以显示TextView和ImageView,此时您只需将这些视图添加到CategoryOption视图本身。

更改为

public class CategoryOption extends LinearLayout {

int iconId;
String optionName;
LinearLayout optionLayout;
TextView tvOption;
ImageView ivIcon;

public CategoryOption(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public CategoryOption(Context context, int iconId, String optionName) {
    super(context);

    this.iconId = iconId;
    this.optionName = optionName;

    tvOption = new TextView(context);
    ivIcon = new ImageView(context);

    setupLayout();
    setupTextView();
    setupImageView();

   addView(ivIcon);
   addView(tvOption);
}

private void setupImageView() {
    // TODO Auto-generated method stub
    LayoutParams params = new LayoutParams(36, 36);
    params.setMargins(0, 0, 0, 4);
    ivIcon.setLayoutParams(params);
    ivIcon.setImageResource(iconId);
}

private void setupLayout() {
    // TODO Auto-generated method stub
    this.setLayoutParams(new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    this.setOrientation(LinearLayout.HORIZONTAL);
    this.setGravity(Gravity.CENTER);
    this.setPadding(4, 4, 4, 4);
    this.setBackgroundColor(Color.parseColor("#DEDEDE"));
}

private void setupTextView() {
    // TODO Auto-generated method stub
    tvOption.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    tvOption.setTextColor(Color.parseColor("#111111"));
    tvOption.setText(optionName);
}
或者删除extends LinearLayout,并使用名为getLayout和return optionLayout的方法

public class CategoryOption  {

int iconId;
String optionName;
LinearLayout optionLayout;
TextView tvOption;
ImageView ivIcon;
Context context;
public CategoryOption(Context context) {
 this.context =context;
    // TODO Auto-generated constructor stub
}

public CategoryOption(Context context, int iconId, String optionName) {
    this.context=context;
    this.iconId = iconId;
    this.optionName = optionName;

    optionLayout = new LinearLayout(context);
    tvOption = new TextView(context);
    ivIcon = new ImageView(context);

}

public LinearLayout getLayout()
{
    LayoutParams params = new LayoutParams(36, 36);
    params.setMargins(0, 0, 0, 4);
    ivIcon.setLayoutParams(params);
    ivIcon.setImageResource(iconId);
    optionLayout.setLayoutParams(new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    optionLayout.setOrientation(LinearLayout.VERTICAL);
    optionLayout.setGravity(Gravity.CENTER);
    optionLayout.setPadding(4, 4, 4, 4);
    optionLayout.setBackgroundColor(Color.parseColor("#DEDEDE"));
    tvOption.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    tvOption.setTextColor(Color.parseColor("#111111"));
    tvOption.setText(optionName);
    optionLayout.addView(ivIcon);
    optionLayout.addView(tvOption);
    return optionLayout;

}
}

你能发布
R.layout.content\u fragment的XML吗
post你的
categoryooption
code做到了。请参阅上面的编辑,其中是onDraw方法,您可以在其中绘制cusotm组件。我只是将一个布局添加到另一个布局中。它非常有效。非常感谢。我忘了我可以将这些视图添加到类本身。@ADeveloper您也可以删除get方法,因为您在构造函数中初始化了iconId和optionname。是的,但我可能需要在用户交互时更改这些视图的属性。
public class CategoryOption  {

int iconId;
String optionName;
LinearLayout optionLayout;
TextView tvOption;
ImageView ivIcon;
Context context;
public CategoryOption(Context context) {
 this.context =context;
    // TODO Auto-generated constructor stub
}

public CategoryOption(Context context, int iconId, String optionName) {
    this.context=context;
    this.iconId = iconId;
    this.optionName = optionName;

    optionLayout = new LinearLayout(context);
    tvOption = new TextView(context);
    ivIcon = new ImageView(context);

}

public LinearLayout getLayout()
{
    LayoutParams params = new LayoutParams(36, 36);
    params.setMargins(0, 0, 0, 4);
    ivIcon.setLayoutParams(params);
    ivIcon.setImageResource(iconId);
    optionLayout.setLayoutParams(new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    optionLayout.setOrientation(LinearLayout.VERTICAL);
    optionLayout.setGravity(Gravity.CENTER);
    optionLayout.setPadding(4, 4, 4, 4);
    optionLayout.setBackgroundColor(Color.parseColor("#DEDEDE"));
    tvOption.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    tvOption.setTextColor(Color.parseColor("#111111"));
    tvOption.setText(optionName);
    optionLayout.addView(ivIcon);
    optionLayout.addView(tvOption);
    return optionLayout;

}
}