在Android中,布局小部件时,fill_parent(match_parent在API级别8和更高级别)和wrap_content?之间有什么区别?
你有什么文件可以指出吗?我很有兴趣了解它.
我有一个包含一对并排按钮的RelativeLayout,我想在布局中居中.我可以将按钮放在LinearLayout中并将其放在RelativeLayout中,但我希望尽可能保持我的xml干净.
这是我尝试过的,这只是将"apply"按钮放在中间,将"undo"按钮放在它的左边:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15sp">
<TextView
android:id="@+id/instructions"
android:text="@string/instructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15sp"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/apply"
android:textSize="20sp"
android:layout_below="@id/instructions"
/>
<Button
android:id="@+id/undo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/undo"
android:textSize="20sp"
android:layout_toLeftOf="@id/apply"
android:layout_below="@id/instructions"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud) 如何将带有id ="naslov"的textview放到中心?我也试过layout_gravity ="center",但这也不起作用.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/naslov"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="Povzetek"
android:gravity="center"/>
<TextView
android:id="@+id/aha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dip"
android:text="Vseh oddaj:"
android:layout_below="@id/naslov"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud) 我正在尝试在React Native选取器中设置项目的文本颜色.到目前为止,我只在iOS上工作,但如果我能找到一个非常棒的跨平台解决方案.
我尝试过以下的事情:
在选择器上的样式颜色
<Picker style={{color:'white'}} ...etc >
Run Code Online (Sandbox Code Playgroud)
在拾取器项目上的样式颜色
<Picker.Item style={{color:'#fff'}} label={'Toronto'} value={'Toronto'} />
Run Code Online (Sandbox Code Playgroud)
我也看过一些添加颜色属性的例子,所以我尝试了这个
<Picker.Item color='white' label={'Toronto'} value={'Toronto'} />
Run Code Online (Sandbox Code Playgroud)
在这里完全失去了.感谢您的任何见解!
编辑:这是一个解决方案 - 在Picker元素中使用itemStyle prop.我相信这只是iOS.
<Picker itemStyle={{color:'white'}} >
<Picker.Item color='white' label={'Toronto'} value={'Toronto'} />
<Picker.Item label={'Calgary'} value={'Calgary'} />
</Picker>
Run Code Online (Sandbox Code Playgroud) 我有这个布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:orientation="horizontal" >
<TextView
android:id="@+id/mon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="10"
android:text="M\nO\nN" />
<CheckBox
android:id="@+id/cbmon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="10" />
<TimePicker
android:id="@+id/timePicker1mon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="40" />
<TimePicker
android:id="@+id/timePicker2mon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="40" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/white"
android:orientation="horizontal" >
<TextView
android:id="@+id/mon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="10"
android:text="T\nU\nE" />
<CheckBox
android:id="@+id/cbtue"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="10" />
<TimePicker
android:id="@+id/timePicker1tue"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="40" />
<TimePicker
android:id="@+id/timePicker2tues"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="40" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" …Run Code Online (Sandbox Code Playgroud) 我一直在研究android xml的.我习惯于android:layout_gravity="center"在特定位置对齐组件.
现在有一天,当我在像Activity这样的Dialog上工作时,我遇到了android:gravity="center".我在那里找到的,gravity用于使其child与特定位置对齐.
我会分享一个例子,
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
<EditText
android:id="@+id/enterNumberEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter No." >
<requestFocus />
</EditText>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
它使LinearLayout中心的孩子对齐.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:background="@drawable/dialog_background" >
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
它将LinearLayout自身对准中心.
现在我的问题是:
gravity适用于子组件并且layout_gravity是父组件.gravity结束layout_gravity.到目前为止,我已经找到了很多关于xml的解释.我想听听更好的解释.如果我错了,请告诉我
我正在尝试创建一个自定义对话框,并将其内容置于中心位置,但它始终以左对齐方式结束.这是aboutdialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:a="http://schemas.android.com/apk/res/android"
a:orientation="vertical"
a:id="@+id/AboutDialogLayout"
a:layout_width="fill_parent"
a:layout_height="fill_parent"
a:layout_gravity="center_horizontal"
a:gravity="center_horizontal">
<ImageView a:id="@+id/imageView1"
a:src="@drawable/pricebook"
a:layout_height="wrap_content"
a:layout_width="wrap_content"/>
<TextView a:layout_height="wrap_content"
a:textAppearance="?android:attr/textAppearanceLarge"
a:id="@+id/textView1"
a:text="Price Book"
a:layout_width="wrap_content"/>
<TextView a:layout_height="wrap_content"
a:id="@+id/textView2"
a:layout_width="wrap_content"
a:text="foo"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的(相关)代码:
Dialog d = new Dialog(this);
d.setContentView(R.layout.aboutdialog);
d.setTitle(R.string.app_name);
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?谢谢你的帮助.
图片:

我的代码出了什么问题,我试图"invalid"在不同的位置(左,右,中)显示我的TextView ,但重力(左,右,中)不起作用!
我的text.xml是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/etext"
android:hint="@string/comment"
android:inputType="textPassword"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100">
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button"
android:layout_weight="25"/>
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="ToggleButton"
android:layout_weight="75"
android:checked="true"
android:paddingLeft="15dp"/>
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/invalid"
android:layout_gravity="center"
android:gravity="center" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的TextPlay.java是
public class TextPlay extends Activity {
Button button;
ToggleButton tbutton;
TextView tview;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
button = …Run Code Online (Sandbox Code Playgroud) 我有一个具有以下布局的活动:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/testlayoutOverlays"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/testlayoutMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/testlayout_bottom"
android:layout_width="fill_parent"
android:layout_height="62dp"
android:background="#122334" >
<ImageView
android:id="@+id/testbtnBlock"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_gravity="left|center_vertical"
android:contentDescription="Test1"
android:padding="@dimen/padding_medium"
android:src="@drawable/btnblock" />
<TextView
android:id="@+id/testtxtZoomPan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/testbtnX"
android:layout_toRightOf="@+id/testbtnBlock"
android:gravity="center_horizontal"
android:text="@string/txtZoomPan"
android:textColor="#FFFFFF" />
<ImageView
android:id="@+id/testbtnX"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_gravity="right|center_vertical"
android:contentDescription="Test2"
android:padding="@dimen/padding_medium"
android:src="@drawable/btnx" />
</RelativeLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/testlayoutPuzzleInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/testlayoutChronoErrors"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Chronometer
android:id="@+id/testchronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:format="@string/chronometer_initial_format"
android:gravity="center" />
<LinearLayout …Run Code Online (Sandbox Code Playgroud) android ×10
layout ×2
xml ×2
customdialog ×1
dialog ×1
gravity ×1
ios ×1
java ×1
javascript ×1
margin ×1
padding ×1
react-native ×1
reactjs ×1
view ×1