单击按钮时更改框架布局的内容

Posted

技术标签:

【中文标题】单击按钮时更改框架布局的内容【英文标题】:Changing the content of framelayout on button click 【发布时间】:2011-07-03 16:46:37 【问题描述】:

没错,我定义了一个主布局,它有一个框架,该框架中有一个表格,然后在底部有一个带有大量按钮的水平滚动视图。单击按钮时,框架的内容应更改为新表格,就像您在选项卡布局教程中选择不同的选项卡时一样(不使用选项卡布局,因为我有很多不适合屏幕的按钮,除非滚动)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_
        android:layout_
        android:padding="5dp">
        <FrameLayout
            android:id="@+id/tabcontent"
            android:layout_            
            android:layout_
            android:layout_weight="1.2"            
            android:padding="5dp" android:background="@color/white">
            <TableLayout android:id="@+id/Jobs" android:layout_ android:layout_ android:stretchColumns="*">
                <TableRow android:id="@+id/JobRow" android:layout_ android:layout_>
                    <TextView android:id="@+id/JobID" android:layout_column="1" android:layout_ android:layout_ android:textColor="@color/black"></TextView>
                    <TextView android:id="@+id/JobStatus" android:layout_column="2" android:layout_ android:layout_ android:textColor="@color/black"></TextView>
                    <TextView android:id="@+id/Customer" android:layout_column="3" android:layout_ android:layout_ android:textColor="@color/black"></TextView>
                    <TextView android:id="@+id/Department" android:layout_column="4" android:layout_ android:layout_ android:textColor="@color/black"></TextView>
                    <TextView android:id="@+id/DocType" android:layout_column="5" android:layout_ android:layout_ android:textColor="@color/black"></TextView>
                </TableRow> 
            </TableLayout>
        </FrameLayout>
        <HorizontalScrollView 
            android:layout_ android:layout_ android:layout_gravity="bottom" android:scrollbars="none">
            <LinearLayout 
                android:orientation="horizontal" android:layout_
                android:layout_>
                <ImageButton android:id="@+id/batching" android:layout_ android:layout_ android:src="@drawable/batching"></ImageButton>
                <ImageButton android:id="@+id/merging" android:layout_ android:layout_ android:src="@drawable/merging"></ImageButton>
                <ImageButton android:id="@+id/processing" android:layout_ android:layout_ android:src="@drawable/processing"></ImageButton>
                <ImageButton android:layout_ android:layout_ android:src="@drawable/processing"></ImageButton>
                <ImageButton android:layout_ android:layout_ android:src="@drawable/processing"></ImageButton>
                <ImageButton android:layout_ android:layout_ android:src="@drawable/processing"></ImageButton>
                <ImageButton android:layout_ android:layout_ android:src="@drawable/processing"></ImageButton>
                <ImageButton android:layout_ android:layout_ android:src="@drawable/processing"></ImageButton>
                <ImageButton android:layout_ android:layout_ android:src="@drawable/processing"></ImageButton>
                <ImageButton android:layout_ android:layout_ android:src="@drawable/processing"></ImageButton>
                <ImageButton android:layout_ android:layout_ android:src="@drawable/processing"></ImageButton>
                <ImageButton android:layout_ android:layout_ android:src="@drawable/processing"></ImageButton>
                <ImageButton android:layout_ android:layout_ android:src="@drawable/processing"></ImageButton>
                <ImageButton android:layout_ android:layout_ android:src="@drawable/processing"></ImageButton>
            </LinearLayout>
        </HorizontalScrollView>
</LinearLayout>

现在对于选项卡,更改选项卡的内容似乎很容易,如下所示:

public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, ArtistsActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, AlbumsActivity.class);
    spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                      res.getDrawable(R.drawable.ic_tab_albums))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, SongsActivity.class);
    spec = tabHost.newTabSpec("songs").setIndicator("Songs",
                      res.getDrawable(R.drawable.ic_tab_songs))
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(2);

如何对框架内容和按钮执行类似操作?我在想一些类似的事情

public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);      

    Resources res = getResources(); // Resource object to get Drawables
    final Intent batchingIntent, mergingIntent, processingIntent;

    // Create an Intent to launch an Activity for the tab (to be reused)        


    final Button batchButton = (Button) findViewById(R.id.batching);
    batchingIntent = new Intent().setClass(this, BatchingActivity.class);
    batchButton.setOnClickListener(new OnClickListener() 
        public void onClick(View v) 
            // Perform action on clicks
            FrameLayout frame = (FrameLayout) findViewById(R.id.tabcontent);
            frame.setContent(batchingIntent) // doesn't exist obviously
        
    );

【问题讨论】:

【参考方案1】:

您不能在 FrameLayout 中嵌套 Activity。你可以做的是用新的观点交换它的观点:


FrameLayout frame = (FrameLayout) findViewById(R.id.tabcontent);
frame.removeAllViews();
frame.addView(aNewViewOne);
frame.addView(aNewViewTwo);
 ...

除了添加视图之外,您还可以将布局膨胀到 FrameLayout:

LayoutInflater.from(context).inflate(R.other_tab, frame, true);

【讨论】:

以上是关于单击按钮时更改框架布局的内容的主要内容,如果未能解决你的问题,请参考以下文章

在代码中更改框架后自动布局重置视图

页面框架内的WPF按钮导航以更改父页面

在 iOS 中单击按钮时的标签和框架扩展

按钮单击小部件时更改布局

单击按钮时更改布局背景颜色

使用框架状态变量更新 JFrame 中的 JPanel 内容