英文:
Sophisticated button layout
问题
我试图在布局中央创建一个大按钮,左侧带有图像,四周有填充,然后垂直居中另外两个不同的文本视图。
我有点新手,非常感谢任何帮助
这是我当前的做法:
<FrameLayout
android:id="@+id/buttonLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="@color/white">
<Button
android:id="@+id/requestButton"
style="@style/request_button"
android:drawableStart="@drawable/card_icon" />
<Button
android:id="@+id/requestTextView"
style="@style/request_title_text_view"
android:text="@string/cards_request" />
<Button
android:id="@+id/bodyTextView"
style="@style/request_body_text_view"
android:text="@string/cards_request_card" />
</FrameLayout>
和样式:
<style name="request_title_text_view" parent="request_body_text_view">
<item name="android:textSize">26sp</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_marginTop">10dp</item>
</style>
<style name="request_body_text_view" parent="android:Widget.TextView">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@color/white</item>
<item name="android:textSize">18sp</item>
<item name="android:textAllCaps">true</item>
<item name="android:paddingRight">20dp</item>
<item name="android:paddingLeft">16dp</item>
<item name="android:layout_marginTop">45dp</item>
<item name="android:layout_marginLeft">80dp</item>
<item name="android:elevation">10dp</item>
</style>
在这里将边距硬编码进样式中不是一个好做法,不知道是否有更好的方式?
英文:
Im trying to create a large button in the center of the layout, with an image on the left,
with padding on all sides, followed by another two different textviews centered vertically
This is what im trying to achieve:
Im kind of new and any help would be greatly appreciated
This is how I do it currently:
<FrameLayout
android:id="@+id/buttonLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="@color/white">
<Button
android:id="@+id/requestButton"
style="@style/request_button"
android:drawableStart="@drawable/card_icon" />
<Button
android:id="@+id/requestTextView"
style="@style/request_title_text_view"
android:text="@string/cards_request"/>
<Button
android:id="@+id/bodyTextView"
style="@style/request_body_text_view"
android:text="@string/cards_request_card" />
</FrameLayout>
And styles:
<style name="request_title_text_view" parent="request_body_text_view">
<item name="android:textSize">26sp</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_marginTop">10dp</item>
</style>
<style name="request_body_text_view" parent="android:Widget.TextView">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@color/white</item>
<item name="android:textSize">18sp</item>
<item name="android:textAllCaps">true</item>
<item name="android:paddingRight">20dp</item>
<item name="android:paddingLeft">16dp</item>
<item name="android:layout_marginTop">45dp</item>
<item name="android:layout_marginLeft">80dp</item>
<item name="android:elevation">10dp</item>
</style>
Its just a bad practice to put those margins there hard-coded and was wondering if there is another better way?
答案1
得分: 1
以下是翻译好的内容:
可以尝试这样做:
<RelativeLayout
<ConstraintLayout
<Button
<ImageView
<TextView
<TextView
</ConstraintLayout
</RelativeLayout
英文:
You can try this:
<RelativeLayout
<ConstraintLayout
<Button
<ImageView
<TextView
<TextView
</ConstraintLayout
</RelativeLayout
答案2
得分: 0
Sure, here is the translated code:
<RelativeLayout
android:id="@+id/requestButton"
android:layout_width="wrap_content"
android:layout_height="88dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="@drawable/button_background"
android:padding="15dp">
<ImageView
android:id="@+id/iconImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="15dp"
android:elevation="10dp"
android:src="@drawable/cr_icon" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@id/iconImageView">
<TextView
android:id="@+id/requestTextView"
style="@style/request_title_text_view"
android:text="@string/cr_request" />
<TextView
android:id="@+id/bodyTextView"
style="@style/request_body_text_view"
android:layout_below="@+id/requestTextView"
android:text="@string/cr_request_body" />
</RelativeLayout>
</RelativeLayout>
Please note that this is the same code you provided, with the HTML escape codes (e.g., <
and "
) replaced with their respective characters in XML.
英文:
<RelativeLayout
android:id="@+id/requestButton"
android:layout_width="wrap_content"
android:layout_height="88dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="@drawable/button_background"
android:padding="15dp">
<ImageView
android:id="@+id/iconImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="15dp"
android:elevation="10dp"
android:src="@drawable/cr_icon" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@id/iconImageView">
<TextView
android:id="@+id/requestTextView"
style="@style/request_title_text_view"
android:text="@string/cr_request" />
<TextView
android:id="@+id/bodyTextView"
style="@style/request_body_text_view"
android:layout_below="@+id/requestTextView"
android:text="@string/cr_request_body" />
</RelativeLayout>
</RelativeLayout>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论