【问题标题】:Placing Rows of Widgets in Android在 Android 中放置小部件行
【发布时间】:2012-01-13 21:00:09
【问题描述】:

我正在尝试使用我的布局文件放置文本视图和按钮行。它应该看起来像这样:

Row 1: [ TextView ( ~2/3 of the screen ) ] [ Button (~1/3 of the screen) ]

Row 2: [ TextView ( ~2/3 of the screen ) ] [ Button (~1/3 of the screen) ]

Row 3: [ TextView ( ~2/3 of the screen ) ] [ Button (~1/3 of the screen) ]

Row 4: [ TextView ( ~2/3 of the screen ) ] [ Button (~1/3 of the screen) ]

Row 5: [ TextView ( ~2/3 of the screen ) ] [ Button (~1/3 of the screen) ]

.....

我对如何做到这一点感到困惑。我可以使用 TableLayout 或 RelativeLayouts 吗?有人可以给我看一些示例代码,因为我不知道如何使用 XML。非常感谢您的帮助!

【问题讨论】:

    标签: java android xml layout widget


    【解决方案1】:

    是的,您可以使用 LinearLayout - android:weightSum 做到这一点,这里是一行的示例,

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:weightSum="0.1" >
    
            <TextView
                android:text="left" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="0.33" /> 
    
            <Button
                android:text="right" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="0.67" />
    
        </LinearLayout>
    


    编辑: 我看到你不明白如何使用它, 假设您有两个布局,(main.xml 和 list_template.xml)

    ma​​in.xml

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/mainLayout"
        >
    
        </LinearLayout>
    </ScrollView>
    

    list_template.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:weightSum="100" >
    
            <TextView
                android:text="left" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="33"
                android:id="@+id/list_textView" /> 
    
            <Button
                android:text="right" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="67" 
                android:id="@+id/list_button" />
    
        </LinearLayout>
    

    list_template 是一行列表的布局模板,我们将它膨胀。怎么样?

    public class StackoverflowActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle b) {
            super.onCreate(b);
            setContentView(R.layout.main);
    
            LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
            LayoutInflater li =  (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            for (int i = 0; i < 5;  i++){
                   View tempView = li.inflate(R.layout.list_template, null);
    
                   TextView list_textView = (TextView) tempView.findViewById(R.id.list_textView);
                   list_textView.setText("TextView"+i);
    
                   Button list_button = (Button) tempView.findViewById(R.id.list_button);
                   list_button.setText("Button"+i);
                   list_button.setId(i);
    
                   list_button.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        //Button clicked
                        Toast.makeText(StackoverflowActivity.this, "Button" + v.getId() + " clicked", Toast.LENGTH_SHORT).show();
                    }
                });
                   mainLayout.addView(tempView);
                }
        }
    }
    

    【讨论】:

    • 澄清一下,这将是您在 ListView 中的行布局。
    • 对不起,我不知道 ListView 是什么。这是否意味着我将在一个完整的 ListView 中有多个 LinearLayout?
    猜你喜欢
    • 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-06-12
    相关资源
    最近更新 更多