如何调整Xamarin中的按钮大小

时间:2016-09-29 12:01:51

标签: xaml mobile xamarin xamarin.forms

我正在使用Xamarin Forms。我试过这段代码,但它不起作用,我怎样才能调整按钮大小?

<ContentView.Content>
  <StackLayout>
    <Button Text="1" WidthRequest="50"></Button>
    <Button Text="2" WidthRequest="50"></Button>
  </StackLayout>
</ContentView.Content>

谢谢。

5 个答案:

答案 0 :(得分:12)

<StackLayout >
    <Button Text="1" WidthRequest="50" 
            HorizontalOptions="Center"></Button>
    <Button Text="2" WidthRequest="50" 
            HorizontalOptions="Center"></Button>
</StackLayout>

enter image description here

答案 1 :(得分:3)

  <StackLayout>
    <!--I am wider but shorter-->
    <Button Text="1" WidthRequest="100" HeightRequest="50"></Button>
    <!--I am narrower but longer-->
    <Button Text="2" WidthRequest="50" HeightRequest="100"></Button>
    <!--I fill the whole width. See also VerticalOptions-->
    <Button Text="3" HorizontalOptions="FillAndExpand"></Button>
  </StackLayout>

答案 2 :(得分:0)

您可以尝试下面提到的方法:

<StackPanel> 
   <Button Content = "Button1" Height = "30" Width = "80" Foreground = "Blue" FontSize = "12" Margin = "10"/>
   <Button Content = "Button2" Height = "30" Width = "80" Foreground = "Blue" FontSize = "12" Margin = "10"/>
   <Button Content = "Button3" Height = "30" Width = "80" Foreground = "Blue" FontSize = "12" Margin = "10"/> 
</StackPanel> 

答案 3 :(得分:0)

Just Size parent View,例如,如果它在StackLayout中,它与父级的大小相同。

Exception ignored in: <bound method Task.__del__ of <Task finished coro=<_run_event() done, defined at /usr/local/lib/python3.5/dist-packages/discord/client.py:304> exception=SystemExit('Stop command run',)>>
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/tasks.py", line 93, in __del__
    futures.Future.__del__(self)
  File "/usr/lib/python3.5/asyncio/futures.py", line 215, in __del__
    self._loop.call_exception_handler(context)
  File "/usr/lib/python3.5/asyncio/base_events.py", line 1177, in call_exception_handler
    exc_info=True)
  File "/usr/lib/python3.5/logging/__init__.py", line 1308, in error
    self._log(ERROR, msg, args, **kwargs)
  File "/usr/lib/python3.5/logging/__init__.py", line 1415, in _log
    self.handle(record)
  File "/usr/lib/python3.5/logging/__init__.py", line 1425, in handle
    self.callHandlers(record)
  File "/usr/lib/python3.5/logging/__init__.py", line 1487, in callHandlers
    hdlr.handle(record)
  File "/usr/lib/python3.5/logging/__init__.py", line 855, in handle
    self.emit(record)
  File "/usr/lib/python3.5/logging/__init__.py", line 1047, in emit
    self.stream = self._open()
  File "/usr/lib/python3.5/logging/__init__.py", line 1037, in _open
    return open(self.baseFilename, self.mode, encoding=self.encoding)
FileNotFoundError: [Errno 2] No such file or directory: '/home/hugernot/Desktop/PVSupportSlave/Code/logs/2018-06-09 17:50:35.563516 (running)/root.log'

它将显示为120 x 120,

因为,Button的默认Horizo​​ntalOptions是Fill,VerticalOptions是Start。但有些控件如按钮,忽略高度或宽度请求,而更喜欢填充父级。同一布局效果按钮中的其他元素。只需调整按钮的父级并保留它。

答案 4 :(得分:0)

您可以使用Horizo​​ntalOptions属性将宽度或高度设置为包含其中的元素所需的大小。

<Button  HorizontalOptions="Center"  Text="Retry" />