尝试以编程方式在相对布局中将视图与RIGHT对齐时遇到问题:
这是xml(我不想更改主要布局,我希望它是线性布局,因为我正在模拟问题):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.upf.ctrl_tp2.Main2Activity"
android:orientation="vertical">
</LinearLayout>
这是mainActivity.java代码:
public class Main2Activity extends AppCompatActivity {
LinearLayout main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
main = (LinearLayout) findViewById(R.id.activity_main2);
TextView txt = new TextView(this);
txt.setText("Hello World!");
txt.setTextSize(20);
RelativeLayout r = new RelativeLayout(this);
RelativeLayout.LayoutParams laypar = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
laypar.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//seems that this doesn't work ?
r.setLayoutParams(laypar);
r.addView(txt);
main.addView(r);
}}
我得到的结果:
我想要的:
答案 0 :(得分:2)
右对齐在线性布局中不起作用。 layout_gravity将起作用。
替换
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 14.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 43363) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="レイヤー_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
y="0px" width="280px" height="184px" viewBox="0 0 280 184" enable-background="new 0 0 280 184" xml:space="preserve">
<rect x="31.833" y="26.885" fill="#FFFFFF" stroke="#000000" width="230.334" height="129.115"/>
<rect id="roomA" x="39.803" y="33.261" fill="#FFFFFF" stroke="#000000" width="44.632" height="37.459"/>
<rect id="roomB" x="91.608" y="33.261" fill="#FFFFFF" stroke="#000000" width="44.632" height="37.459"/>
<rect id="roomC" x="145.406" y="33.261" fill="#FFFFFF" stroke="#000000" width="44.632" height="37.459"/>
<rect id="roomD" x="198.806" y="33.261" fill="#FFFFFF" stroke="#000000" width="44.632" height="37.459"/>
<rect id="roomE" x="39.803" y="106.984" fill="#FFFFFF" stroke="#000000" width="44.632" height="37.459"/>
<rect id="roomF" x="91.608" y="106.984" fill="#FFFFFF" stroke="#000000" width="44.632" height="37.459"/>
<rect id="roomG" x="145.406" y="106.984" fill="#FFFFFF" stroke="#000000" width="44.632" height="37.459"/>
<rect id="roomH" x="198.806" y="106.984" fill="#FFFFFF" stroke="#000000" width="44.632" height="37.459"/>
<text transform="matrix(1 0 0 1 58.9814 54.6021)" font-family="'KozGoPro-Regular-90ms-RKSJ-H'" font-size="9.5641">A</text>
<text transform="matrix(1 0 0 1 110.7866 54.6021)" font-family="'KozGoPro-Regular-90ms-RKSJ-H'" font-size="9.5641">B</text>
<text transform="matrix(1 0 0 1 166.3115 54.6021)" font-family="'KozGoPro-Regular-90ms-RKSJ-H'" font-size="9.5641">C</text>
<text transform="matrix(1 0 0 1 219.1797 54.6021)" font-family="'KozGoPro-Regular-90ms-RKSJ-H'" font-size="9.5641">D</text>
<text transform="matrix(1 0 0 1 58.9814 129.2549)" font-family="'KozGoPro-Regular-90ms-RKSJ-H'" font-size="9.5641">E</text>
<text transform="matrix(1 0 0 1 110.7866 129.2549)" font-family="'KozGoPro-Regular-90ms-RKSJ-H'" font-size="9.5641">F</text>
<text transform="matrix(1 0 0 1 166.3115 129.2549)" font-family="'KozGoPro-Regular-90ms-RKSJ-H'" font-size="9.5641">G</text>
<text transform="matrix(1 0 0 1 217.5859 129.2549)" font-family="'KozGoPro-Regular-90ms-RKSJ-H'" font-size="9.5641">H</text>
<text transform="matrix(1 0 0 1 33.1611 23.9634)" font-family="'KozGoPro-Regular-90ms-RKSJ-H'" font-size="9.5641">FloorPlan</text>
</svg>
<br>
<span id="room1" data-id="roomA" class='roomList'>RoomA</span><br>
<span id="room2" data-id="roomB" class='roomList'>RoomB</span><br>
<span id="room3" data-id="roomC" class='roomList'>RoomC</span><br>
<span id="room4" data-id="roomD" class='roomList'>RoomD</span><br>
<span id="room5" data-id="roomE" class='roomList'>RoomE</span><br>
<span id="room6" data-id="roomF" class='roomList'>RoomF</span><br>
<span id="room7" data-id="roomG" class='roomList'>RoomG</span><br>
<span id="room8" data-id="roomH" class='roomList'>RoomH</span><br>
<span id="room9" data-id="roomI" class='roomList'>RoomI</span><br>
用这个
laypar.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//seems that this doesn't work ?
答案 1 :(得分:1)
将LayoutParam
设置为TextView
。
txt.setLayoutParams(laypar);
您也可以在xml中使用android:layout_alignParentRight="true
。
答案 2 :(得分:0)
是的,MR。 @CrazyDeveloper所有看起来都不错的东西只有你要说的一件事。
RelativeLayout r = new RelativeLayout(this);
// actul issue belove .
RelativeLayout.LayoutParams laypar = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
只需改变它。
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
宽度 MATCH_PARENT 而不是 WRAP_CONTENT
答案 3 :(得分:0)
试试这个:
main = (LinearLayout) findViewById(R.id.ll_layout);
TextView txt = new TextView(this);
txt.setText("Hello World!");
txt.setTextSize(20);
RelativeLayout r = new RelativeLayout(this);
RelativeLayout.LayoutParams laypar = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
laypar.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//seems that this doesn't work ?
r.setGravity(Gravity.RIGHT);
r.setLayoutParams(laypar);
r.addView(txt);
ll_layout.addView(r);