Xamarin音频播放器菜单始终在顶部并展开

x7rlezfr  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(95)

我正在开发一个安卓和iOS应用程序,用来从我的电台收听音乐。现在我有一个底部带有菜单选项的选项卡式页面设计。在MainActivity中,我创建了一个ContentView,它的linearLayout总是位于所有标签页的顶部。但我希望球员可以扩大到顶部和扭转。
我做了一个视频截图的另一个应用程序从荷兰的什么我想实现。

的数据
最大的问题是,TabbedPage能够做到这一点,还是我需要一个完整的其他设置?
不同种类的linearLayouts或relativeLayouts
根据ToolmakerSteve的要求添加了代码。
在我的MainActivity OnCreate中,我有以下内容:

ViewGroup contentView = FindViewById<ViewGroup>(Android.Resource.Id.Content);
                LinearLayout view = new LinearLayout(this);
                view.Orientation = Android.Widget.Orientation.Horizontal;
                view.SetBackgroundColor(Android.Graphics.Color.Blue);
                view.Background = getDrawableWithRadius();
                FrameLayout.LayoutParams parameter = new FrameLayout.LayoutParams((Device.Idiom == TargetIdiom.Tablet ? 60 : LinearLayout.LayoutParams.MatchParent), (Device.Idiom == TargetIdiom.Tablet ? 110 : 160));
                parameter.Gravity = GravityFlags.Bottom | GravityFlags.Left;
                parameter.BottomMargin = 220;
                view.LayoutParameters = parameter;

                btnPushPlay = new Android.Widget.ImageButton(this);
                btnPushPlay.Click += BtnPushPlay_Click;
   btnPushPlay.SetBackgroundColor(Android.Graphics.Color.Transparent);
                btnPushPlay.SetImageResource(Resource.Drawable.media_play_green);
                view.AddView(btnPushPlay);
                lblSong = new TextView(this);
                lblSong.TextSize = 18;
                lblSong.SetPadding(10, 35, 0, 0);
                lblSong.SetTextColor(Android.Graphics.Color.White);
                lblSong.Text = "Luister live!";
                view.AddView(lblSong);
                contentView.AddView(view);

字符串
在MainPage.xaml中,我有这个:

<?xml version="1.0" encoding="utf-8" ?>
    <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:local="clr-namespace:Kickit_Radio.MenuPages;assembly=Kickit_Radio"
            xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
            xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:local1="clr-namespace:Kickit_Radio.ViewModels"
            android:TabbedPage.ToolbarPlacement="Bottom"
            BarBackgroundColor="#000000"
            BarTextColor="White"
            BackgroundColor="Black"
            x:Class="Kickit_Radio.MainPage"
            NavigationPage.HasNavigationBar="False"
            NavigationPage.HasBackButton="True" SelectedTabColor="Green" UnselectedTabColor="#3184ca">
    <TabbedPage.Children>
        <local:Home Title="Home" IconImageSource="home.ico" />
        <local:Nieuws Title="Nieuws" IconImageSource="@drawable/news" />
        <local:Request Title="Request" IconImageSource="@drawable/record" />
        <local:Programma Title="Programma" IconImageSource="@drawable/column" />
        <local:Streams Title="Streams" IconImageSource="@drawable/record" />
        <local:Contact Title="Contact" IconImageSource="@drawable/about"/>
    </TabbedPage.Children>
</TabbedPage>

t3psigkw

t3psigkw1#

两个技巧。

#1:在两个视图之间切换本机媒体播放器的Android代码

MainActivity.cs:

// This must be a member of the app class, so it exists after OnCreate finishes.
LinearLayout view;   // small view
ViewGroup bigView;   // large view

... OnCreate()
{
    
    // Change this:
    //LinearLayout view = new LinearLayout(this);
    // To this:
    view = new LinearLayout(this);
    ...
    btnPushPlay = new Android.Widget.ImageButton(this);
    ...
    contentView.AddView(view);

    // Using Android XML layout (".xml" resource file, not XAML), define an Android view that contains native Android MediaPlayer.
    bigView = ...;
    // TODO: set Gravity to Top.
    ...
    // TBD: I think we can add it now, but either ".Gone" or ".Invisible".
    bigView.Visibility = Android.Views.ViewStates.Gone;   // Not there at first.
    contentView.AddView(bigView);   // Or maybe do this later, when want it.
}

private void ToggleMediaPlayer(bool large)
{
    if (large)
    {
        view.Visibility = Android.Views.ViewStates.Gone;
        bigView.Visibility = Android.Views.ViewStates.Visible;
    }
    else
    {
        view.Visibility = Android.Views.ViewStates.Visible;
        bigView.Visibility = Android.Views.ViewStates.Gone;
    }
}

// TODO: btnPushPlay needs a click method that calls `ToggleMediaPlayer(true);`.
// TODO: bigView must have a button that calls `ToggleMediaPlayer(false);`.

字符串

#2:全部使用Xamarin.表单

  • 删除OnCreate中创建view并将其添加到内容的代码。
  • 请使用Community Toolkit / TabView,而不要使用TabbedPage。
  • 使用XF Grid将小视图和大视图叠加在具有TabView的页面顶部。

MainPage.xaml:

<!-- change from TabbedPage to ContentPage -->
<ContentPage ...>
  <!-- outer grid, with three overlaid children. -->
  <Grid RowDefinitions="*" ...>

    <!-- replacement for TabbedPage -->
    <!-- https://github.com/xamarin/XamarinCommunityToolkit/blob/main/samples/XCT.Sample/Pages/Views/TabView/CustomTabsPage.xaml -->
    <Grid x:Name="tabview" ...>
      <TabView ...>
        ...
      </TabView>
    </Grid>

    <!-- small view overlay -->
    <!-- TODO: OR maybe this should become a new row in "tabview" Grid above. -->
    <Grid x:Name="smallView" RowDefinitions="*,Auto" ...>
        <!-- nothing in Row 0: it is empty space -->

        <!-- small view at bottom of screen -->
        <StackLayout Grid.Row="1" Orientation="Horizontal" ...>
            ...
            <ImageButton x:Name="btnPushPlay" .../>
            ...
        </StackLayout>
    </Grid>

    <!-- large view overlay -->
    <Grid x:Name="bigView" IsVisible="False" ...>
        ...
    </Grid>

  </Grid>
</ContentPage>


在主页面.xaml.cs中:

// Change from ": TabbedPage" to "ContentPage".
public partial class MainPage : ContentPage
{

  private void ToggleMediaPlayer(bool large)
  {
    bigView.IsVisible = large;

    // This probably isn't needed, if bigView covers the entire screen.
    //MAYBE smallView.IsVisible = !large;
  }

// TODO: btnPushPlay needs a click method that calls `ToggleMediaPlayer(true);`.
// TODO: bigView must have a button that calls `ToggleMediaPlayer(false);`.


你得研究一下,把遗漏的细节补上。并根据您的需要进行调整。

相关问题