This commit is contained in:
fengjignqi
2026-11-13 13:51:44 +08:00
parent 2a1e77e866
commit 77549b1f4e
291 changed files with 34817 additions and 35 deletions

View File

@@ -0,0 +1,187 @@
from pathlib import Path
import xml.etree.ElementTree as ET
from xml.sax.saxutils import quoteattr
RES = Path(__file__).resolve().parents[1] / 'app/src/main/res'
NS = {'android': 'http://schemas.android.com/apk/res/android',
'app': 'http://schemas.android.com/apk/res-auto',
'tools': 'http://schemas.android.com/tools'}
def attr(key):
if '__' in key:
prefix, name = key.split('__', 1)
return '{' + NS[prefix] + '}' + name
if key in ('style', 'layout'):
return key
return '{' + NS['android'] + '}' + key
def node(tag, parent=None, width='match_parent', height='wrap_content', **attrs):
element = ET.Element(tag)
for key, value in dict(layout_width=width, layout_height=height, **attrs).items():
element.set(attr(key), str(value))
if parent is not None:
parent.append(element)
return element
def text(parent, resource=None, style='FoodTitle', **attrs):
if resource:
attrs['text'] = '@string/' + resource
return node('TextView', parent, style='@style/' + style, **attrs)
def card(parent=None, **attrs):
return node('androidx.cardview.widget.CardView', parent,
app__cardBackgroundColor='@android:color/white',
app__cardCornerRadius='10dp', app__cardElevation='0dp',
app__cardPreventCornerOverlap='false', **attrs)
def write(path, root, comment):
def render(e, depth=0):
pad = ' ' * depth
values = [(k, v) for k, v in e.attrib.items()]
values.sort(key=lambda pair: (0 if pair[0] == attr('id') else 1 if pair[0] == 'style' else 2))
if depth == 0:
values = [('xmlns:' + k, v) for k, v in NS.items()] + values
lines = [pad + '<' + e.tag]
for key, value in values:
for prefix, url in NS.items():
key = key.replace('{' + url + '}', prefix + ':')
lines.append(pad + ' ' + key + '=' + quoteattr(value))
if len(e):
lines[-1] += '>'
for child in e:
lines += ['', render(child, depth + 1)]
lines += [pad + '</' + e.tag + '>']
else:
if len(lines) == 1: lines[0] += ' />'
else: lines[-1] += ' />'
return '\n'.join(lines)
(RES / path).write_text('<?xml version="1.0" encoding="utf-8"?>\n<!-- ' + comment + ' -->\n'
+ render(root) + '\n', encoding='utf-8')
# 推荐与全部美食复用同一卡片,一行和两行文字自然形成不同高度。
recommend = card(id='@+id/ll_food_recommend', layout_marginHorizontal='5.5dp',
layout_marginBottom='10dp', clickable='true', focusable='true',
foreground='?attr/selectableItemBackground')
body = node('androidx.constraintlayout.widget.ConstraintLayout', recommend)
node('ImageView', body, width='0dp', height='0dp', id='@+id/iv_food_cover',
scaleType='centerCrop', src='@drawable/food_cover_placeholder', contentDescription='@null',
app__layout_constraintDimensionRatio='167:125', app__layout_constraintStart_toStartOf='parent',
app__layout_constraintEnd_toEndOf='parent', app__layout_constraintTop_toTopOf='parent')
text(body, style='FoodItemName', id='@+id/tv_food_name', width='0dp', minHeight='33dp',
paddingHorizontal='8dp', paddingVertical='8dp', maxLines='2', ellipsize='end',
app__layout_constraintTop_toBottomOf='@id/iv_food_cover',
app__layout_constraintStart_toStartOf='parent', app__layout_constraintEnd_toEndOf='parent',
app__layout_constraintBottom_toBottomOf='parent', tools__text='Injera FirfirInjera FirfirInjera Firfir')
write('layout/food_recommend_item.xml', recommend, '白色圆角美食卡片:封面按比例缩放,文案最多两行,超出末尾省略。')
breakfast = card(id='@+id/ll_food_breakfast', width='120dp', layout_marginEnd='10dp',
clickable='true', focusable='true', foreground='?attr/selectableItemBackground')
body = node('LinearLayout', breakfast, orientation='vertical')
node('ImageView', body, height='90dp', id='@+id/iv_food_cover', scaleType='centerCrop',
src='@drawable/food_cover_placeholder', contentDescription='@null')
text(body, style='FoodItemName', id='@+id/tv_food_name', minHeight='33dp',
paddingHorizontal='8dp', paddingVertical='8dp', maxLines='2', ellipsize='end', tools__text='Injera Firfir')
write('layout/food_breakfast_item.xml', breakfast, '横向美食卡片,白色文案底板随内容增高,不固定文字区域高度。')
category = node('TextView', width='wrap_content', id='@+id/tv_food_category',
style='@style/LoginDesignText', minHeight='33dp', layout_marginEnd='6dp',
paddingHorizontal='20dp', paddingVertical='8dp', gravity='center',
background='@drawable/food_category_bg', textColor='@color/food_category_text',
textSize='14sp', maxLines='1', clickable='true', focusable='true',
tools__text='@string/food_category_breakfast')
write('layout/food_category_item.xml', category, '分类标签沿用列表选中状态,文字放大时允许高度随内容增高。')
banner = node('FrameLayout', height='match_parent', id='@+id/fl_food_banner',
clickable='true', focusable='true', foreground='?attr/selectableItemBackground')
node('ImageView', banner, height='match_parent', id='@+id/iv_food_banner',
scaleType='centerCrop', src='@drawable/food_cover_placeholder', contentDescription='@null')
node('ImageView', banner, height='34dp', layout_gravity='bottom',
src='@mipmap/bg_food_banner_scrim', scaleType='fitXY', contentDescription='@null')
node('LinearLayout', banner, width='wrap_content', height='4dp', id='@+id/ll_banner_indicators',
layout_gravity='bottom|end', layout_marginEnd='12dp', layout_marginBottom='12dp',
orientation='horizontal', importantForAccessibility='no')
write('layout/food_banner_item.xml', banner, '轮播图仅保留图片与右下角指示器,不叠加商品标题及价格。')
# 首页只承载业务区域,状态栏和底部导航继续由 MainActivity 处理。
home = node('com.scwang.smart.refresh.layout.SmartRefreshLayout', height='match_parent',
id='@+id/refresh_shop', background='@color/food_page_background')
scroll = node('androidx.core.widget.NestedScrollView', home, height='match_parent',
id='@+id/sv_food_content', background='@color/food_page_background',
fillViewport='true', scrollbars='none')
frame = node('FrameLayout', scroll)
content = node('com.ckkj.griddle.custom.view.AdaptiveContentLayout', frame,
layout_gravity='top|center_horizontal', app__maxContentWidth='1000dp',
orientation='vertical', paddingBottom='16dp')
text(content, 'food_title', id='@+id/tv_food_title', layout_marginHorizontal='24dp',
layout_marginTop='30dp', minHeight='31dp', gravity='start|center_vertical', textSize='26sp')
banner_frame = node('androidx.constraintlayout.widget.ConstraintLayout', content,
layout_marginHorizontal='24dp', layout_marginTop='10dp')
banner_card = card(banner_frame, width='0dp', height='0dp', id='@+id/cv_food_banner',
app__layout_constraintDimensionRatio='345:194',
app__layout_constraintStart_toStartOf='parent', app__layout_constraintEnd_toEndOf='parent',
app__layout_constraintTop_toTopOf='parent', app__layout_constraintBottom_toBottomOf='parent')
banner_card.set(attr('app__cardCornerRadius'), '16dp')
node('androidx.viewpager2.widget.ViewPager2', banner_card, height='match_parent',
id='@+id/vp_food_banner', tools__layout='@layout/food_banner_item')
node('androidx.recyclerview.widget.RecyclerView', content, id='@+id/rv_food_categories',
layout_marginTop='16dp', paddingStart='24dp', paddingEnd='18dp', clipToPadding='false',
orientation='horizontal', scrollbars='none', nestedScrollingEnabled='false',
app__layoutManager='androidx.recyclerview.widget.LinearLayoutManager',
tools__itemCount='4', tools__listitem='@layout/food_category_item')
for title_id, resource, see_id, rv_id, item in [
('tv_food_section_title', 'food_category_breakfast', 'tv_food_category_see_all',
'rv_food_breakfast', 'food_breakfast_item'),
('tv_food_recommend_title', 'food_recommend', 'tv_food_recommend_see_all',
'rv_food_recommend', 'food_recommend_item'),
]:
heading = node('LinearLayout', content, minHeight='32dp', layout_marginHorizontal='24dp',
layout_marginTop='14dp', orientation='horizontal', gravity='center_vertical')
text(heading, resource, id='@+id/' + title_id, width='0dp', layout_weight='1')
text(heading, 'food_see_all', 'FoodSeeAll', id='@+id/' + see_id, width='wrap_content',
minHeight='32dp', drawableEnd='@mipmap/icon_food_see_all', drawablePadding='5dp')
breakfast_list = rv_id == 'rv_food_breakfast'
rv = node('androidx.recyclerview.widget.RecyclerView', content, id='@+id/' + rv_id,
layout_marginTop='5dp', nestedScrollingEnabled='false', scrollbars='none',
tools__itemCount='4', tools__listitem='@layout/' + item)
if breakfast_list:
for key, value in dict(paddingStart='24dp', paddingEnd='14dp', clipToPadding='false',
orientation='horizontal',
app__layoutManager='androidx.recyclerview.widget.LinearLayoutManager').items():
rv.set(attr(key), value)
else:
rv.set(attr('layout_marginHorizontal'), '18.5dp')
# 瀑布流管理器由 Fragment 初始化,避免 XML 提前创建普通网格。
write('layout/shop_fragment.xml', home, '新版美食首页:大标题、按比例轮播、横向分类与卡片、推荐瀑布流。')
# 查看全部保留现有数据绑定标题和刷新控件,商城 Tab 的独立布局保持不变。
path = RES / 'layout/all_goods_layout.xml'
root = ET.parse(path).getroot()
outer = root.find('LinearLayout')
outer.set(attr('background'), '@color/white')
title = outer.find('include')
title.attrib.pop(attr('layout_marginTop'), None)
title.set(attr('background'), '@color/white')
title.set(attr('app__title'), '@{@string/food_all_recipes_title}')
title.set(attr('app__titleTextColor'), '@{@color/food_text}')
area = outer.find('FrameLayout')
area.set(attr('background'), '@color/food_page_background')
refresh = area.find('com.scwang.smart.refresh.layout.SmartRefreshLayout')
area.remove(refresh)
adaptive = node('com.ckkj.griddle.custom.view.AdaptiveContentLayout', width='match_parent',
height='match_parent', layout_gravity='center_horizontal',
app__maxContentWidth='1000dp', orientation='vertical')
adaptive.append(refresh)
area.insert(0, adaptive)
rv = refresh.find('androidx.recyclerview.widget.RecyclerView')
rv.set(attr('paddingStart'), '18.5dp')
rv.set(attr('paddingEnd'), '18.5dp')
rv.set(attr('paddingTop'), '20dp')
rv.set(attr('paddingBottom'), '14dp')
rv.set(attr('tools__listitem'), '@layout/food_recommend_item')
write('layout/all_goods_layout.xml', root, '全部美食:白色标题栏与状态栏,独立刷新分页的瀑布流列表。')