2015-04-01 83 views
0

我想知道如何格式化按钮,以便它们都显示在屏幕上而不是关闭它?我有一个10x10的网格,也希望它们压缩在一起,没有间距。我目前正在用Xamarin在Visual StudiOS中写这个。在Android中使用按钮进行网格布局的问题

protected override void OnCreate(Bundle bundle) 
{ 
    base.OnCreate(bundle); 
    SetContentView(Resource.Layout.Main); 
    GridLayout gl = (GridLayout)FindViewById(Resource.Id.GridLayout1); 
    // Set our view from the "main" layout resource 
    Button[] cells = new Button[99]; 
    var i = 0; 
    foreach (Button b in cells) 
    { 
     cells[i] = new Button(this); 
     gl.AddView(cells[i]); 
     i++; 
    } 
}} 
<?xml version="1.0" encoding="utf-8"?> 

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 

xmlns:tools="http://schemas.android.com/tools" 

android:id="@+id/GridLayout1" 

android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:columnCount="10" 
android:rowCount="10" 
android:orientation="horizontal" 
tools:context=".GridXMLActivity" /> 
using System; 
using Android.App; 

using Android.Content; 

using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.OS; 

Namespace App3 { 

[Activity(Label = "App3", MainLauncher = true, Icon = "@drawable/icon")] 
Public class MainActivity : Activity 
{ 

我现在正在得到什么。 http://i.stack.imgur.com/zkBjF.png

+0

请具体说明你的问题,你得到的网格布局,但也有空格按键之间,但不这样做需要他们吗? – Keshav1234 2015-04-01 06:34:53

+0

是的,我不能得到水平行上的所有10个按钮。我认为android:layout_width =“match_parent”可能会这样做,但似乎没有。我有一张imgur照片链接在我目前正在获取的文章中。 – 2015-04-01 07:02:15

+0

您是否尝试过使用水平滚动视图? – Keshav1234 2015-04-01 07:04:56

回答

0

尝试通过如下图所示,这可能帮助你水平滚动视图:

<HorizontalScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

      <!-- Your content here --> 

    </HorizontalScrollView>