【问题标题】:adding a dynamic linearlayout to the current view android向当前视图android添加动态线性布局
【发布时间】:2013-01-10 13:14:03
【问题描述】:

我正在尝试将线性布局添加到滚动视图 这是我的代码 代码可以编译,但没有显示新布局

这是原始布局(我想添加到它)

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" 

android:layout_margin="15dp"
android:layout_marginTop="15dp">

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" 
     android:id="@+id/ViewHistoryImageLayout">

    <ImageView
        android:id="@+id/HistoryImage"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="0.76"
        android:gravity="center"
        android:padding="10dp"
        android:src="@drawable/upload" 
        android:contentDescription="@string/HistoryImage"/>

    <TextView
        android:id="@+id/TranslatedText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.12"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="@string/translateImageButton" />

</LinearLayout>

这是我想多次添加的布局:

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<RatingBar
    android:id="@+id/ratingBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:numStars="5" />

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

</LinearLayout>

添加新布局的java代码是:

setContentView(R.layout.activity_view_history_image);
ScrollView sv = new ScrollView(this);
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View ll = inflater.inflate(R.layout.translation_menu, null);
sv.addView(ll);

代码编译正常,应用程序正在运行,但没有任何反应 .xml 文件之一有问题吗?

tnx

【问题讨论】:

    标签: android android-layout android-linearlayout android-scrollview


    【解决方案1】:

    你应该使用充气机, 改变:

    LinearLayout ll = new LinearLayout(R.layout.translation_menu);
    

    LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    View ll = inflater.inflate(R.layout.translation_menu, null);
    sv.addView(ll);
    

    LayoutInflater 从相应的 XML 文件创建一个 View 对象。

    【讨论】:

    • tnx!什么是变量上下文?
    • 您需要一个 Context 对象来检索调用 getSystemService。如果你在一个活动中,你可以直接调用 getSystemService
    • 哦,是的,我使用了 getApplicationContect() 但它不起作用,代码编译并且应用程序正在运行,但我没有看到任何添加到布局中的东西可以是原始的东西吗布局?
    【解决方案2】:
      LayoutInflater li = LayoutInflater.from(context);
      LinearLayout ll = (LinearLayout) li.inflate(R.layout.translation_menu, this)
    

    这是正确的方法。

    【讨论】:

      【解决方案3】:

      使用LayoutInflator

      LayoutInflater 类用于将布局 XML 文件实例化为其对应的 View 对象。

      换句话说,它将一个 XML 文件作为输入,并从中构建视图对象。

      ScrollView sv = new ScrollView(this);
      LayoutInflater layoutInflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );   
      LinearLayout ll = (LinearLayout) li.inflate(translation_menu, this);  
      sv.addView(ll);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-25
        相关资源
        最近更新 更多