- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
有没有办法实现类似于 RelativeLayout
的东西?在 Android 上可以吗?
特别是我正在寻找类似于 centerInParent
的内容, layout_below:<layout_id>
, layout_above:<layout_id>
, 和 alignParentLeft
更多关于RelativeLayout的引用:https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
编辑:这是一个依赖于 RelativeLayout
的布局示例
所以在上图中,顶部的“tofu's Songs”文本对齐为 centerInParent
在 RelativeLayout
内.而另外两个是 alignParentLeft
和 alignParentRight
在每个单元格上,火图标所在的位置,其底部的点赞数围绕火焰图标的中心对齐。此外,每个单元格的顶部和底部标题分别与图像头像的右侧和顶部和底部对齐。
最佳答案
Flutter 布局通常使用 Column
的树构建。 , Row
和 Stack
小部件。这些小部件采用构造函数参数,指定子级相对于父级的布局规则,您还可以通过将它们包装在 Expanded
中来影响单个子级的布局。 , Flexible
, Positioned
, Align
, 或 Center
小部件。
也可以使用 CustomMultiChildLayout
构建复杂的布局。 .这就是 Scaffold
在内部实现,如何在应用程序中使用它的示例出现在 Shrine demo .您也可以使用LayoutBuilder
或 CustomPaint
,或向下一层扩展RenderObject
如sector example所示.像这样手动进行布局需要更多的工作,并且会在极端情况下产生更多错误的可能性,所以如果可以的话,我会尝试使用高级布局原语。
回答您的具体问题:
AppBar
使用leading
和trailing
参数定位您的应用栏元素。如果您想改用 Row
,请使用 MainAxisAlignment.spaceBetween
的 mainAxisAlignment
。Row
使用 CrossAxisAlignment.center
的 crossAxisAlignment
来定位下方的火图标和编号。Column
使用 MainAxisAlignment.spaceBetween
的 mainAxisAlignment
来定位您的顶部和底部标题。 (您应该考虑使用 ListTile
来布置列表图 block ,但这样做会失去对精确定位的控制。)这是实现您提供的设计的代码 fragment 。在这个例子中,我使用了 IntrinsicHeight
确定歌曲拼贴的高度,但您可以通过将其硬编码为固定高度来提高性能。
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
brightness: Brightness.dark,
primaryColorBrightness: Brightness.dark,
),
home: new HomeScreen(),
debugShowCheckedModeBanner: false,
);
}
}
class Song extends StatelessWidget {
const Song({ this.title, this.author, this.likes });
final String title;
final String author;
final int likes;
@override
Widget build(BuildContext context) {
TextTheme textTheme = Theme
.of(context)
.textTheme;
return new Container(
margin: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 10.0),
decoration: new BoxDecoration(
color: Colors.grey.shade200.withOpacity(0.3),
borderRadius: new BorderRadius.circular(5.0),
),
child: new IntrinsicHeight(
child: new Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Container(
margin: const EdgeInsets.only(top: 4.0, bottom: 4.0, right: 10.0),
child: new CircleAvatar(
backgroundImage: new NetworkImage(
'http://thecatapi.com/api/images/get?format=src'
'&size=small&type=jpg#${title.hashCode}'
),
radius: 20.0,
),
),
new Expanded(
child: new Container(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Text(title, style: textTheme.subhead),
new Text(author, style: textTheme.caption),
],
),
),
),
new Container(
margin: new EdgeInsets.symmetric(horizontal: 5.0),
child: new InkWell(
child: new Icon(Icons.play_arrow, size: 40.0),
onTap: () {
// TODO(implement)
},
),
),
new Container(
margin: new EdgeInsets.symmetric(horizontal: 5.0),
child: new InkWell(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Icon(Icons.favorite, size: 25.0),
new Text('${likes ?? ''}'),
],
),
onTap: () {
// TODO(implement)
},
),
),
],
),
),
);
}
}
class Feed extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new ListView(
children: [
new Song(title: 'Trapadelic lobo', author: 'lillobobeats', likes: 4),
new Song(title: 'Different', author: 'younglowkey', likes: 23),
new Song(title: 'Future', author: 'younglowkey', likes: 2),
new Song(title: 'ASAP', author: 'tha_producer808', likes: 13),
new Song(title: '🌲🌲🌲', author: 'TraphousePeyton'),
new Song(title: 'Something sweet...', author: '6ryan'),
new Song(title: 'Sharpie', author: 'Fergie_6'),
],
);
}
}
class CustomTabBar extends AnimatedWidget implements PreferredSizeWidget {
CustomTabBar({ this.pageController, this.pageNames })
: super(listenable: pageController);
final PageController pageController;
final List<String> pageNames;
@override
final Size preferredSize = new Size(0.0, 40.0);
@override
Widget build(BuildContext context) {
TextTheme textTheme = Theme
.of(context)
.textTheme;
return new Container(
height: 40.0,
margin: const EdgeInsets.all(10.0),
padding: const EdgeInsets.symmetric(horizontal: 20.0),
decoration: new BoxDecoration(
color: Colors.grey.shade800.withOpacity(0.5),
borderRadius: new BorderRadius.circular(20.0),
),
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: new List.generate(pageNames.length, (int index) {
return new InkWell(
child: new Text(
pageNames[index],
style: textTheme.subhead.copyWith(
color: Colors.white.withOpacity(
index == pageController.page ? 1.0 : 0.2,
),
)
),
onTap: () {
pageController.animateToPage(
index,
curve: Curves.easeOut,
duration: const Duration(milliseconds: 300),
);
}
);
})
.toList(),
),
);
}
}
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => new _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
PageController _pageController = new PageController(initialPage: 2);
@override
build(BuildContext context) {
final Map<String, Widget> pages = <String, Widget>{
'My Music': new Center(
child: new Text('My Music not implemented'),
),
'Shared': new Center(
child: new Text('Shared not implemented'),
),
'Feed': new Feed(),
};
TextTheme textTheme = Theme
.of(context)
.textTheme;
return new Stack(
children: [
new Container(
decoration: new BoxDecoration(
gradient: new LinearGradient(
begin: FractionalOffset.topCenter,
end: FractionalOffset.bottomCenter,
colors: [
const Color.fromARGB(255, 253, 72, 72),
const Color.fromARGB(255, 87, 97, 249),
],
stops: [0.0, 1.0],
)
),
child: new Align(
alignment: FractionalOffset.bottomCenter,
child: new Container(
padding: const EdgeInsets.all(10.0),
child: new Text(
'T I Z E',
style: textTheme.headline.copyWith(
color: Colors.grey.shade800.withOpacity(0.8),
fontWeight: FontWeight.bold,
),
),
)
)
),
new Scaffold(
backgroundColor: const Color(0x00000000),
appBar: new AppBar(
backgroundColor: const Color(0x00000000),
elevation: 0.0,
leading: new Center(
child: new ClipOval(
child: new Image.network(
'http://i.imgur.com/TtNPTe0.jpg',
),
),
),
actions: [
new IconButton(
icon: new Icon(Icons.add),
onPressed: () {
// TODO: implement
},
),
],
title: const Text('tofu\'s songs'),
bottom: new CustomTabBar(
pageController: _pageController,
pageNames: pages.keys.toList(),
),
),
body: new PageView(
controller: _pageController,
children: pages.values.toList(),
),
),
],
);
}
}
最后说明:在本例中,我使用了常规 AppBar
,但您也可以使用带有固定 SliverAppBar
的 CustomScrollView
elevation
为 0.0。这将使内容在您的应用栏后面滚动时可见。让它与 PageView
很好地配合是很棘手的,因为它需要一个固定大小的区域来布置自己。
关于android - Flutter中RelativeLayout的等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44396075/
我正在尝试将我的应用布局从 LinearLayout 切换到 RelativeLayout。对我来说,保持良好的代码结构很重要,我认为如果我有一个父级 RelativeLayyout 用于整个屏幕,然
我正在尝试动态构建由多个图像组成的相关布局。这些相对布局将显示在先前相对布局的下方/右侧。 我从两个相对布局(rl100 和 rl200)开始。 rl200 低于 rl100。但是 rl200 并不低
作为一名 Android 开发人员,我是一个新手,我目前正在了解 RelativeLayout,但面临一个较小的问题,即 Textview 在图像上重叠。在这里,我为您提供了我想要的和我得到的图像以及
我有一个 ListView,其中填充了 RelativeLayouts。这些 RelativeLayouts 的内容可以改变,从而分别改变它们的高度。在每个 RelativeLayout 上,我在布局
我有一个 RelativeLayout,这个 Layout 有两个 child ,一个是 MapView,另一个是包含按钮的 RelativeLayout。 我希望它看起来像那样 但是我的透明框(Re
RelativeLayout in Android Studio 我有一个水平的 LinearLayout,其中有一个 RelativeLayout,我希望另一个 RelativeLayout 位于其
我的 Activity 布局必须如下所示: 屏幕分为 3 个部分(3“行”) 第一部分由左侧的图像和旁边的两个 TextView 组成。 第二部分是一个大文本,放入 ScrollView 中以便滚动。
我有一个如下的 relativeLayout: 在java代码中,我想在listview的左边添加一个view,但是没有成功: m_relativeLayout = (RelativeL
我想通过拖动 ImageView/RelativeLayout 的角来增加 ImageView/RelativeLayout 的大小。但在宽度上没有差异,并将其设置为底部。我通过使用 android:
我正在尝试获取我的 RelativeLayout 的尺寸。 许多人告诉我使用以下代码: RelativeLayout relativeLayout = (RelativeLayout) fin
我还没有找到解决我的问题的方法,也许你可以在这里帮助我。 我正在使用带有 ImageView 和 TextView 作为 subview 的 RelativeLayout。 TextView 包含大文
我有一个带有 ImageView 和 TextView 的 RelativeLayout。我想要 TextView 位于 ImageView 正下方(带有小填充)以及 ImageView 和 Text
每次单击按钮时,我都尝试在新 Activity (已创建)中添加一个新的 TextView,但我也遇到了多个错误,例如空指针异常和非法状态异常。 这是我在 button Activity 中使用的代码
有没有办法将高斯模糊效果应用于 View ?我想将此效果应用于另一个布局之上的相对布局。我找到了this ,但它仅适用于图像。P.S.:相对布局有纯色(我使用了setBackgroundColor方法
我有一个益智游戏,其中的棋盘是一个矩形,边框由一个网格单元长的棋子组成。我有一种随机化图像并将它们环绕在关卡周围的方法,但是......这真的很糟糕。它有一个最奇怪的错误:根据运行的 Android
我正在尝试创建一个拼字游戏,您可以在拼字游戏中向右或向左拖动一个字母,然后交换字母。以编程方式对 RelativeLayout 中的项目重新排序的最佳方法是什么,以便在向左拖动字母时,tile 通过的
我正在添加 accessebility支持一些应用程序。它适用于标准 UI 元素(例如按钮),但由于某种原因不适用于我的自定义元素,即带有 ImageView 和 TextView 的 Relativ
我正在从事一个涉及在图像上绘画的项目。 要删除不需要的直线或曲线,我必须绘制一个边框和 X 按钮将其删除。 我有一个相对布局,其中有手绘 Canvas 。在编辑模式下,我应该像你在图片中看到的那样制作
我在 android 中为 relativelayout 编写了一些代码,但没有 xml,下面是我使用的代码。 package layout.program; import android.app.
我有一个使用相对布局的表格行设置。它有 2 个 TextView,我希望它们具有相等的间距和左对齐的文本。请参阅下面的当前设置。这有一个 TextView 左对齐,另一个右对齐。当我更改为对齐左侧内容
我是一名优秀的程序员,十分优秀!