首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >水平滚动视图一个子项(Eclipse IDE,Android)

水平滚动视图一个子项(Eclipse IDE,Android)
EN

Stack Overflow用户
提问于 2013-11-19 15:21:45
回答 2查看 261关注 0票数 0

我意识到有很多相关的问题,但我正在寻找这个具体的答案。下面的代码是我在应用程序中的主要活动的XML。现在我的问题来了。我希望能够动态地改变水平视图的内容。这将涉及到清除它的视图以及添加新视图。当我试图给线性布局分配一个ID时,我得到了一个错误,说它不能接受字符串类型的id。

代码语言:javascript
复制
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

        <TextView
           android:id="@+id/txtClassification"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentLeft="true"
           android:layout_alignParentTop="true"
           android:text="Classification:&lt;>"
           android:textAppearance="?android:attr/textAppearanceLarge"
           tools:ignore="HardcodedText" />

        <Spinner
           android:id="@+id/spinGoto"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentBottom="true"
           android:layout_alignParentLeft="true"
           android:layout_marginBottom="56dp" />

       <Button
           android:id="@+id/btnBack"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentBottom="true"
           android:layout_alignParentRight="true"
           android:text="Go Back"
           tools:ignore="HardcodedText" />

        <Button
           android:id="@+id/btnSelect"
           android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:text="Select"
            tools:ignore="HardcodedText" />

        <HorizontalScrollView
            android:id="@+id/gallery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/txtClassification"
            android:layout_alignRight="@+id/spinGoto"
            android:layout_below="@+id/txtClassification" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal" >

            </LinearLayout>
        </HorizontalScrollView>

    <ImageView
        android:id="@+id/myImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

    </RelativeLayout>

非常感谢大家的帮助!

EN

回答 2

Stack Overflow用户

发布于 2013-11-19 15:36:14

你将不得不使用 LinearLayout,因为一个HorizontalScrollView只能有一个直接子对象。

代码语言:javascript
复制
<HorizontalScrollView
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtClassification" >

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

        </LinearLayout>
    </HorizontalScrollView>

然后以编程方式访问它以添加或删除视图

代码语言:javascript
复制
public void addView(View v) {
    LinearLayout yourLayout = (LinearLayout) findViewById(R.id.yourLayout);
    yourLayout.addView(v);
}

public void clearLayout() {
    LinearLayout yourLayout = (LinearLayout) findViewById(R.id.yourLayout);
    yourLayout.removeAllViews();
}

public void removeView(View v) {
    LinearLayout yourLayout = (LinearLayout) findViewById(R.id.yourLayout);
    yourLayout.removeView(v);
}

希望这能有所帮助

票数 0
EN

Stack Overflow用户

发布于 2013-11-19 15:41:13

id具有预定义的格式。在"@+id/"语法后输入所需的名称。示例android:id="@+id/layout"

然后在代码中获取HorizontalScrollView的LinearLayoutinside:

代码语言:javascript
复制
LinearLayout layout = findViewById(R.id.layout);

要将视图添加到布局,请调用布局的adView()。示例:

代码语言:javascript
复制
//add 5 buttons
for (int i = 1; i <= 5 ; i++){
Button btn = new Button(this);
btn.setId(i);
btn.setText(i+"");
layout.addView(btn);
}

要从布局中删除视图,请调用removeView()

代码语言:javascript
复制
//remove button nr. 3
Button btn3 = layout.findViewById(3);
layout.removeView(btn3);

HorizontalScrollView负责处理所有事情,您只需填充和组织嵌套的LinearLayout中的元素。

祝好运!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20065499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档